Here is the complete solution:
function isPowerOfTwo(n)
return n > 0 and (n & (n - 1)) = 0
The check excludes zero and negative numbers. The expression ensures exactly one 1 bit. Time: . Space: . This is the kind of elegant bit trick that impresses interviewers.