Google
Information Storage and Retrieval: Fastest way to know if a number is power of 2

Pages

Saturday, August 16, 2014

Fastest way to know if a number is power of 2

Using Bitwise operators:

Check num & (num-1) == 0. If true, the number is a power of 2 else its not.

No comments: