tags:

views:

152

answers:

3

Using the classic code snippet:

if (x & (x-1)) == 0

If the answer is 1, then it is false and not a power of 2. However, working on 5 (not a power of 2) and 4 results in:

0001 1111 0001 1111 0000 1111

That's 4 1s.

Working on 8 and 7:

1111 1111 0111 1111

0111 1111

The 0 is first, but we have 4.

In this link (http://www.exploringbinary.com/ten-ways-to-check-if-an-integer-is-a-power-of-two-in-c/) for both cases, the answer starts with 0 and there is a variable number of 0s/1s. How does this answer whether the number is a power of 2?

+5  A: 

You need refresh yourself on how binary works. 5 is not represented as 0001 1111 (5 bits on), it's represented as 0000 0101 (2^2 + 2^0), and 4 is likewise not 0000 1111 (4 bits on) but rather 0000 0100 (2^2). The numbers you wrote are actually in unary.

Wikipedia, as usual, has a pretty thorough overview.

Tyler McHenry
Good link. That helps me understand how the solution to this problem works. :)
dotnetdev
+4  A: 

Any power of two number can be represent in binary with a single 1 and multiple 0s.

eg. 
10000(16) 
1000(8) 
100(4)

If you subtract 1 from any power of two number, you will get all 1s to the right of where the original one was.

10000(16) - 1 = 01111(15)

ANDing these two numbers will give you 0 every time.

In the case of a non-power of two number, subtracting one will leave at least one "1" unchanged somewhere in the number like:

10010(18) - 1 = 10001(17)

ANDing these two will result in

10000(16) != 0
Joel Potter
+1  A: 

Keep in mind that if x is a power of 2, there is exactly 1 bit set. Subtract 1, and you know two things: the resulting value is not a power of two, and the bit that was set is no longer set. So, when you do a bitwise and &, every bit that was set in x is not unset, and all the bits in (x-1) that are set must be matched against bits not set in x. So the and of each bit is always 0.

In other words, for any bit pattern, you are guaranteed that (x&(x-1)) is zero.

Charlie Martin