Skip to main content

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.

Problems43 / 152
Sign in

Power of Two

EASYMathBit Manipulation

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.

Draft saves automatically

Loading editor...

Submit also runs hidden tests
n =
1
Expected
true
Run with your own input (JSON)