       Re: Re: Number of Differing Digits & Another Problem (want to

• To: mathgroup at smc.vnet.net
• Subject: [mg76388] Re: [mg76340] Re: [mg76310] Number of Differing Digits & Another Problem (want to
• From: DrMajorBob <drmajorbob at bigfoot.com>
• Date: Mon, 21 May 2007 05:58:43 -0400 (EDT)
• References: <4047165.1179646026504.JavaMail.root@m35>
• Reply-to: drmajorbob at bigfoot.com

```Clear[bitDifference]
bitDifference[a_, a_] = 0;
bitDifference[a_, b_] /; a > b :=
Block[{one = IntegerDigits[a, 2], two},
two = IntegerDigits[b, 2, Length@one];
Plus @@ Abs[one - two]
]
bitDifference[a_, b_] := bitDifference[b, a]

Bobby

On Sun, 20 May 2007 01:23:41 -0500, Bob Hanlon <hanlonr at cox.net> wrote:

> Since you intend to count the bit differences your function needs to b=
e  =

> modified to
>
> Clear[BitDifferences];
> BitDifferences[a_,b_,n_]:=
>     n-Count[Equal@@@
>           Thread[{IntegerDigits[a,2,n],IntegerDigits[b,2,n]}],True];
>
> t1=Table[BitDifferences[a,b,6],{a,-16,16},{b,-16,16}];
>
> I would use
>
> Clear[BitDifferences];
> BitDifferences[a_,b_]:=Module[
>       {n=Length[IntegerDigits[Max[Abs[{a,b}]],2]]},
>       n-Count[IntegerDigits[a,2,n]-IntegerDigits[b,2,n],0]];
>
> t2=Table[BitDifferences[a,b],{a,-16,16},{b,-16,16}];
>
> t1==t2
>
> True
>
>
> Bob Hanlon
>
> ---- 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).
>>
>> I want to see how you would do it so I can broaden my Mathematica
>> perspective.
>>
>> Thanks.
>>
>>
>
>
>

-- =

DrMajorBob at bigfoot.com

```

• Prev by Date: Re: change \$UserDocumentsDirectory
• Next by Date: Re: Re: Number of Differing Digits & Another
• Previous by thread: Re: Number of Differing Digits & Another Problem (want to
• Next by thread: Re:  LinearAlgebra`MatrixManipulation` LinearEquationsToMatrices