- Reason
- 2^4 = 16
Check Power of 2 in Python
What you’ll learn
- What powers of two are and how to recognize them quickly.
- How the bitwise trick
n & (n - 1)works. - How to test one value and print powers of two in a range.
Overview
Powers of two are numbers like 1, 2, 4, 8, 16... They have one set bit in binary form.
Bitwise method
Fast O(1) check using one expression.
Live preview
Try 16, 12, 1, and 0 instantly.
Range output
Print powers of two from 1 to 20.
Prerequisites
Basic Python conditionals and bitwise AND operator.
- Know integer binary representation at a basic level.
- Understand
n > 0guard for this check.
What is a power of 2?
A power of two has form 2^k with k >= 0. Sequence starts 1, 2, 4, 8, 16.
In binary, powers of two have exactly one 1 bit.
Why n & (n - 1) works
Subtracting 1 flips the lowest set bit and all bits to its right. For positive powers of two with one set bit, AND becomes zero.
1000 & 0111 = 0000, so 8 passes.
Quick examples
- Reason
- Between 8 and 16
- Reason
- 2^0 = 1
Live preview
Uses positive-check plus bitwise rule.
Algorithm
Goal: determine if n is 2^k for some k >= 0.
Reject n <= 0
Only positive numbers are considered here.
Apply bitwise test
If (n & (n - 1)) == 0, n is power of two.
📜 Pseudocode
function is_power_of_two_bitwise(n):
if n <= 0:
return false
return (n & (n - 1)) == 0
function is_power_of_two_loop(n):
if n <= 0:
return false
while n > 1 and n mod 2 == 0:
n = n / 2
return n == 1 Check a single number
Fast bitwise check in Python.
def is_power_of_two(num: int) -> bool:
return num > 0 and (num & (num - 1)) == 0
number = 16
if is_power_of_two(number):
print(f"{number} is a power of 2.")
else:
print(f"{number} is not a power of 2.") Powers of 2 from 1 to 20
Reuse helper and print all powers in range.
def is_power_of_two(num: int) -> bool:
return num > 0 and (num & (num - 1)) == 0
print("Power of 2 in the range 1 to 20:")
for i in range(1, 21):
if is_power_of_two(i):
print(i, end=" ") Notes
Equivalent style. Some write num and not (num & (num - 1)).
Loop fallback. Repeated divide-by-2 method avoids bitwise syntax for beginners.
Interview phrase. "Positive integer with exactly one set bit."
❓ FAQ
🔄 Input / output examples
Replace the number in example 1 to test quickly.
| Input number | Typical line |
|---|---|
| 16 | 16 is a power of 2. |
| 12 | 12 is not a power of 2. |
| 1 | 1 is a power of 2. |
| 0 | 0 is not a power of 2. |
Edge cases
Not power of two
Fails n > 0 guard.
Reject immediately
This tutorial treats negatives as not powers of two.
Bitwise and logical ops
Use & inside expression and boolean and for guard.
⏱️ Time and space complexity
| Approach | Time (single n) | Extra space |
|---|---|---|
| Bitwise n & (n - 1) | O(1) | O(1) |
| Divide-by-2 loop | O(log n) | O(1) |
| Range scan [1, U] | O(U) | O(1) |
Summary
- Power of two means one set bit in positive integer.
- Use
n > 0 and (n & (n - 1)) == 0for fast check. - Range listing reuses the same helper cleanly.
Many computing sizes are powers of two, so this check appears in memory alignment, bit masks, and tree/heap questions.
8 people found this page helpful
