Decide whether a positive integer is an exact power of two (1, 2, 4, 8, 16, …).
Input
A single positive integer n.
Output
true if n is a power of two, otherwise false.
Decide whether a positive integer is an exact power of two (1, 2, 4, 8, 16, …).
A single positive integer n.
true if n is a power of two, otherwise false.
Example 1
1
true
1 is 2^0.
Example 2
16
true
16 is 2^4.
Example 3
18
false
18 = 10010 in binary — two bits set.
A power of two has exactly one bit set in binary.
For a power of two, n AND (n-1) clears that one bit and leaves 0.
A power of two has a single set bit, so n > 0 and (n & (n - 1)) == 0.
n = int(input())
print("true" if n > 0 and (n & (n - 1)) == 0 else "false")
Tell us what needs to change, who it affects and any important deadline. We will review the context and reply with useful next questions.