A power of two has exactly one bit set:
Method: n & (n - 1) clears the lowest set bit. If the result is , there was only one bit.
function isPowerOfTwo(n):
return n > 0 and (n & (n - 1)) == 0
Example:
- . . . True.
- . . . False.
Note: Check because is not a power of two but .
Time: .