Checking for powers of two source code

This snippet submitted by Webmaster on 2005-01-26. It has been viewed 33963 times.
Rating of 5.7 with 323 votes

/* The following code uses bitwise operators to determine 
if an unsigned integer, x, is a power of two.  If x is a power of two, x is represented in binary
 with only a single bit; therefore, subtraction by one removes that bit and flips all the 
lower-order bits.  The bitwise and <> 

then effectively checks to see if any bit is the 
same.  If not, then it's a power of two.*/

int powerOfTwo(unsigned int x)
    return !((x-1) & x);

More C and C++ source code snippets

Add a snippet!