Statement▼
An integer n is considered a power of two if it can be expressed as n
Determine whether a given integer n is a power of two. If it is, then return TRUE; otherwise, return FALSE.
Constraints:
−231≤ n≤231−1
Solution
The main idea of this solution is to use bit manipulation to check whether a number contains only one set bit in its binary representation. Powers of two are always positive and have exactly one bit set to 1 from n turns the rightmost 1-bit into n and n - 1 gives zero only for powers of two, confirming that the number contains exactly one set bit.