Problem Statement
Explain how to check whether an integer is a power of two using bit manipulation.
Explanation
An integer >0 is a power of two if exactly one bit is set. So x>0 and (x & (x-1))==0. This clears the lowest set bit, so if only one bit was set it becomes zero. Time O(1).
Code Solution
SolutionRead Only
boolean isPowerOfTwo(int x){ return x>0 && (x & (x-1))==0; }