A quick and nifty check to see if a number is a power of two is to note that for numbers that are a power of two, you’ll get a number which is all ones. For example:

1000 -1 0111

Note that the resulting number has no bits in common with the original number; this property is **only** true for powers of two. For any other number, bits are left standing.

Thus, we can test for power-of-two-ness by:

1 2 3 | inline bool ispowof2(int x){ return (x&(x-1))==0; } |

Which is probably the fastest possible way to do this.