Power of Two

07/16/2016 Math Bit Manipulation

Question

Given an integer, write a function to determine if it is a power of two.

Solution

Result: Accepted Time: 4 ms

Here should be some explanations.

bool  isPowerOfTwo(const int n) {
return  n > 0 &&n==((-n)&(n));
}


Complexity Analytics

• Time Complexity: $O(1)$
• Space Complexity: $O(1)$