Question

Requirement: O(1) time

Solution

Do a special handle for input = 0. source

bool IsPowerOfTwo(ulong x) {
    return (x & (x - 1)) == 0;
}