Re: Number of Differing Digits & Another Problem (want to see different ways it can be done)
- To: mathgroup at smc.vnet.net
- Subject: [mg76378] Re: [mg76310] Number of Differing Digits & Another Problem (want to see different ways it can be done)
- From: Andrzej Kozlowski <akoz at mimuw.edu.pl>
- Date: Sun, 20 May 2007 02:43:32 -0400 (EDT)
- References: <200705190845.EAA21281@smc.vnet.net>
On 19 May 2007, at 17:45, 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. > > It would be simpler to use simply: Count[IntegerDigits[a, 2, n] - IntegerDigits[b, 2, n], 0] but then the user has to specify the padding n himself and the function gives the number of places in which they integers do not differ (which depends on the padding). And by the way: IntegerDigits[6, 2] {1, 1, 0} IntegerDigits[5, 2] {1, 0, 1}] so they differ in two places (or do not differ in just one place). One example of code that realy computes the number of places in which binary representations of two integers differ (without having to choose BitDifferences[a_, b_] := With[{n = Max[Length[IntegerDigits[a, 2]], Length[IntegerDigits[b, 2]]]}, n - Count[IntegerDigits[a, 2, n] - IntegerDigits[b, 2, n], 0]] BitDifferences[5, 6] 2 Andrzej Kozlowksi
- References:
- Number of Differing Digits & Another Problem (want to see different ways it can be done)
- From: VenDiddy@gmail.com
- Number of Differing Digits & Another Problem (want to see different ways it can be done)