int ispowerof2(unsigned int x) {
return x && !(x & (x - 1));
}
Note that the bit pattern of a power of two is of the form 10...0 and that of a number just one less is 011...1.
int ispowerof2(unsigned int x) {
return x && !(x & (x - 1));
}
Note that the bit pattern of a power of two is of the form 10...0 and that of a number just one less is 011...1.