Power of Two - Implementation

Here is the implementation:

function isPowerOfTwo(n):
    return n > 0 and (n & (n - 1)) == 0

This runs in O(1)O(1) time. No loops needed.

Alternative: n > 0 && (highestOneBit(n) == n) or check if n is in the set {1,2,4,8,...}\{1, 2, 4, 8, ...\}.