Web Toolbar by Wibiya

Pages

Sunday, June 19, 2011

What does ((n&(n-1)) == 0) checks??

Solution: A number and its predecessor when &ed will result in '0' only when there is just one '1' bit in the number eg. 1000 and its predecessor 0111. So, this condition checks whether the number is a multiple of 2 or not.

1 comment:

bharat said...

Hi,
n&n-1 actually resets the first least significant bit which is set. If its 0 doesnt it mean its a power of 2.