Menu
CoddyTech

Power of Two

You get an integer n. Return true if n is a power of two, meaning n = 2^k for some whole number k ≥ 0, and false otherwise. So 1, 2, 4 and 8 count, while 0, 6 and every negative number do not.

Function

isPowerOfTwo(n: integer) → boolean
ninteger
the integer to test, which may be zero or negative
Returnsboolean
true if n equals 2^k for some k ≥ 0, false otherwise

Constraints

  • -231 ≤ n ≤ 231-1

Examples

Input
n = 16
Output
true
Explanation
16 = 2 × 2 × 2 × 2 = 2^4. In binary it is 10000, a single 1 bit.

lock icon+17 hidden tests on Submit

challenge icon

Follow-up

With the same bit tricks, can you test whether n is a power of four without a loop?

Reset code
def isPowerOfTwo(n):
    # Write code here
Test cases

Case 1

Case 2

Case 3

Input

n = 16

Expected

true