A Developer's Diary

Jul 15, 2008

Code to check if a number can be expressed in the form 2^n

The bit representation of any number of the form 2^n will have only one field set.
If we do an AND operation with the (number-1) the result is 0
i.e (n & n-1) is 0

The number is of form 2^n if (n & n-1) is 0


Read more ...