Python Program to Check if Number is Power of 2
You can check if a number is a power of 2 in Python using
n > 0 and (n & (n - 1)) == 0, which returns True if n is a power of 2.Examples
Input16
OutputTrue
Input18
OutputFalse
Input1
OutputTrue
How to Think About It
To check if a number is a power of 2, think about its binary form. A power of 2 has exactly one '1' bit in binary. For example, 8 is 1000 in binary. If you subtract 1 from it, you get 7, which is 0111. When you do a bitwise AND between the number and one less than it, the result is zero only if the number is a power of 2.
Algorithm
1
Get the input number n.2
Check if n is greater than zero.3
Perform bitwise AND between n and n-1.4
If the result is zero, return True (n is power of 2).5
Otherwise, return False.Code
python
def is_power_of_two(n): return n > 0 and (n & (n - 1)) == 0 # Test examples print(is_power_of_two(16)) # True print(is_power_of_two(18)) # False print(is_power_of_two(1)) # True
Output
True
False
True
Dry Run
Let's trace the number 16 through the code
1
Check if n > 0
n = 16, which is greater than 0, so continue
2
Calculate n & (n - 1)
16 in binary is 10000, 15 in binary is 01111 Bitwise AND: 10000 & 01111 = 00000
3
Compare result to zero
Result is 0, so return True
| n | n-1 | n & (n-1) | Result |
|---|---|---|---|
| 16 | 15 | 0 | True |
Why This Works
Step 1: Check positive number
The number must be greater than zero because powers of 2 are positive.
Step 2: Bitwise AND operation
Using n & (n - 1) clears the lowest set bit. If the result is zero, only one bit was set.
Step 3: Result means power of 2
If the bitwise AND result is zero, the number has exactly one '1' bit, so it is a power of 2.
Alternative Approaches
Using math.log2 and is_integer
python
import math def is_power_of_two(n): return n > 0 and math.log2(n).is_integer() print(is_power_of_two(16)) # True print(is_power_of_two(18)) # False
This method uses logarithms but can be slower and less precise for large numbers.
Using a loop to divide by 2
python
def is_power_of_two(n): if n <= 0: return False while n % 2 == 0: n //= 2 return n == 1 print(is_power_of_two(16)) # True print(is_power_of_two(18)) # False
This method is simple but slower for large numbers because it uses a loop.
Complexity: O(1) time, O(1) space
Time Complexity
The bitwise operation runs in constant time because it uses a fixed number of CPU instructions.
Space Complexity
No extra memory is needed; the check is done in-place with constant space.
Which Approach is Fastest?
The bitwise method is fastest and simplest compared to logarithm or loop methods.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Bitwise AND | O(1) | O(1) | Fastest and simplest |
| Logarithm | O(1) | O(1) | Readable but slower, may have precision issues |
| Loop division | O(log n) | O(1) | Easy to understand, slower for large numbers |
Use the bitwise method
n & (n - 1) == 0 for the fastest and simplest check.Forgetting to check if the number is greater than zero before applying the bitwise check.