views:

189

answers:

3

To test if an unsigned integer is of the form 2^n-1 we use:

x&(x+1)

What is that supposed to equal? That is,

x&(x+1) == ?
+7  A: 

A number of the form 2^n-1 will have all of the bits up to the nth bit set. For example, 2^3-1 (7) is:

0b0111

If we add one to this, we get 8:

0b1000

Then, performing a bitwise and, we see that we get zero, because no bit is set on in both numbers. If we start with a number not of the form 2^n+1, then the result will be nonzero.

James McNellis
I think you meant `2^n-1` at the beginning
Michael Mrozek
@Michael: Quite right. Thank you.
James McNellis
thanks everybody
Pascal Cuoq
@Pascal: If you'd like to answer and go into greater detail, I'd be happy to upvote such an answer. :-)
James McNellis
@James I went ahead and drafted a short explanation.
Pascal Cuoq
+5  A: 

Zero. If X is 2^N-1, it is an unbroken string of 1's in binary. One more than that is a 1 followed by a string of zeroes same length as X, so the two numbers have no 1 bits in common in any location, so the AND of the two is zero.

JustJeff
+6  A: 

In complement to the existing answers, here is a short explanation of why numbers x that are not of the form 0b00000 (zero) or 0b0111..11 (all lowest digits set, these are all the numbers 2^n-1 for n>0) do not have the property x&(x+1) == 0.

For a number x of the form 0b????1000..00, x+1 has the same digits as x except for the least significant bit, so x & (x+1) has at least one bit set, the bit that was displayed as being set in x. By way of shorter explanation:

x       0b????1000..00
x+1     0b????1000..01
x&(x+1) 0b????10000000

For a number x of the form 0b????10111..11:

x       0b????10111..11
x+1     0b????110000000
x&(x+1) 0b????10000..00

In conclusion, if x is not either zero or written in binary with all lowest digits set, then x&(x+1) is not zero.

Pascal Cuoq
+1 because both sides of the explanation are important!
psmears