Power of Two
Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2^x.
Power of Two
Given an integer n, return true if it is a power of two. Otherwise, return false. An integer n is a power of two, if there exists an integer x such that n == 2^x.
Example 1
Input: n = 1 Output: true
Example 2
Input: n = 16 Output: true
Show hint
A power of 2 has only one bit set. Check if n > 0 and (n & (n-1)) == 0. Or repeatedly divide by 2 until you get 1 or an odd number.
Sign in to submit, save progress and mark problems as solved. You can still run your code.
Loading editor...
n =
1
Expected
true