Given a number x, find next number with same number of 1 bits in it’s binary representation.

For example, consider x = 12, whose binary representation is 1100 (excluding leading zeros on 32 bit machine). It contains two logic 1 bits. The next higher number with two logic 1 bits is 17 (100012).

Algorithm:

When we observe the binary sequence from 0 to 2n – 1 (n is # of bits), right most bits (least significant) vary rapidly than left most bits. The idea is to find right most string of 1’s in x, and shift the pattern to right extreme, except the left most bit in the pattern. Shift the left most bit in the pattern (omitted bit) to left part of x by one position. An example makes it more clear,

x = 156

10

x = 10011100

(2)

10011100
00011100 - right most string of 1's in x
00000011 - right shifted pattern except left most bit ------> [A]
00010000 - isolated left most bit of right most 1's pattern
00100000 - shiftleft-ed the isolated bit by one position ------> [B]
10000000 - left part of x, excluding right most 1's pattern ------> [C]
10100000 - add B and C (OR operation) ------> [D]
10100011 - add A and D which is required number 163

(10)

After practicing with few examples, it easy to understand. Use the below given program for generating more sets.

[ad type=”banner”]

Program Design:

We need to note few facts of binary numbers. The expression x & -x will isolate right most set bit in x (ensuring x will use 2’s complement form for negative numbers). If we add the result to x, right most string of 1’s in x will be reset, and the immediate ‘0’ left to this pattern of 1’s will be set, which is part [B] of above explanation. For example if x = 156, x & -x will result in 00000100, adding this result to x yields 10100000 (see part D). We left with the right shifting part of pattern of 1’s (part A of above explanation).

There are different ways to achieve part A. Right shifting is essentially a division operation. What should be our divisor? Clearly, it should be multiple of 2 (avoids 0.5 error in right shifting), and it should shift the right most 1’s pattern to right extreme. The expression (x & -x) will serve the purpose of divisor. An EX-OR operation between the number X and expression which is used to reset right most bits, will isolate the rightmost 1’s pattern.

Note that we are adding right most set bit to the bit pattern. The addition operation causes a shift in the bit positions. The weight of binary system is 2, one shift causes an increase by a factor of 2. Since the increased number (rightOnesPattern in the code) being used twice, the error propagates twice. The error needs to be corrected. A right shift by 2 positions will correct the result.

The popular name for this program is same number of one bits.

[pastacode lang=”c” manual=”%23include%3Ciostream%3E%0A%20%0Ausing%20namespace%20std%3B%0A%20%0Atypedef%20unsigned%20int%20uint_t%3B%0A%20%0A%2F%2F%20this%20function%20returns%20next%20higher%20number%20with%20same%20number%20of%20set%20bits%20as%20x.%0Auint_t%20snoob(uint_t%20x)%0A%7B%0A%20%0A%20%20uint_t%20rightOne%3B%0A%20%20uint_t%20nextHigherOneBit%3B%0A%20%20uint_t%20rightOnesPattern%3B%0A%20%0A%20%20uint_t%20next%20%3D%200%3B%0A%20%0A%20%20if(x)%0A%20%20%7B%0A%20%0A%20%20%20%20%2F%2F%20right%20most%20set%20bit%0A%20%20%20%20rightOne%20%3D%20x%20%26%20-(signed)x%3B%0A%20%0A%20%20%20%20%2F%2F%20reset%20the%20pattern%20and%20set%20next%20higher%20bit%0A%20%20%20%20%2F%2F%20left%20part%20of%20x%20will%20be%20here%0A%20%20%20%20nextHigherOneBit%20%3D%20x%20%2B%20rightOne%3B%0A%20%0A%20%20%20%20%2F%2F%20nextHigherOneBit%20is%20now%20part%20%5BD%5D%20of%20the%20above%20explanation.%0A%20%0A%20%20%20%20%2F%2F%20isolate%20the%20pattern%0A%20%20%20%20rightOnesPattern%20%3D%20x%20%5E%20nextHigherOneBit%3B%0A%20%0A%20%20%20%20%2F%2F%20right%20adjust%20pattern%0A%20%20%20%20rightOnesPattern%20%3D%20(rightOnesPattern)%2FrightOne%3B%0A%20%0A%20%20%20%20%2F%2F%20correction%20factor%0A%20%20%20%20rightOnesPattern%20%3E%3E%3D%202%3B%0A%20%0A%20%20%20%20%2F%2F%20rightOnesPattern%20is%20now%20part%20%5BA%5D%20of%20the%20above%20explanation.%0A%20%0A%20%20%20%20%2F%2F%20integrate%20new%20pattern%20(Add%20%5BD%5D%20and%20%5BA%5D)%0A%20%20%20%20next%20%3D%20nextHigherOneBit%20%7C%20rightOnesPattern%3B%0A%20%20%7D%0A%20%0A%20%20return%20next%3B%0A%7D%0A%20%0Aint%20main()%0A%7B%0A%20%20int%20x%20%3D%20156%3B%0A%20%20cout%3C%3C%22Next%20higher%20number%20with%20same%20number%20of%20set%20bits%20is%20%22%3C%3Csnoob(x)%3B%0A%20%0A%20%20getchar()%3B%0A%20%20return%200%3B%0A%7D” message=”c” highlight=”” provider=”manual”/] [ad type=”banner”]