Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Number of Differing Digits & Another Problem (want to see different ways it can be done)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg76375] Re: Number of Differing Digits & Another Problem (want to see different ways it can be done)
  • From: Bill Rowe <readnewsciv at sbcglobal.net>
  • Date: Sun, 20 May 2007 02:41:57 -0400 (EDT)

On 5/19/07 at 4:45 AM, VenDiddy at gmail.com wrote:

>I just purchased a copy of Mathematica and I've been learning it for
>about a week now. You can expect that I will be posting a lot of
>questions. One thing I've noticed is that there are so many
>different ways to do the same thing!

>Here is a function I came up with that calculates how many binary
>digits two numbers differ in:

>BitDifferences[a_, b_, n_] :=
>Count[Equal @@@ Thread[{IntegerDigits[a, 2, n], IntegerDigits[b, 2,
>n]}], True]

>For example 5 = 101 differs from 6 = 111 by one digit (the middle
>digit).

111 base 2 = 7 not 6

If you want the number of different binary digits, you need to
count the instances where the digits are not the same, i.e., you
need to replace True with False in the above code.

I would write this function as follows

BitDifferences[a_Integer,b_Integer]:=Total@IntegerDigits[BitXor[a,
b], 2]
--
To reply via email subtract one hundred and four


  • Prev by Date: Re: [6] LinearAlgebra`MatrixManipulation` LinearEquationsToMatrices
  • Next by Date: Re: Number of Differing Digits & Another Problem (want to see different ways it can be done)
  • Previous by thread: Re: Number of Differing Digits & Another Problem (want to see different ways it can be done)
  • Next by thread: Re: Number of Differing Digits & Another Problem (want to see different ways it can be done)