Check Power of 2 in Python

Beginner
⏱️ 11 min read
📚 Updated: May 2026
🎯 2 Code Examples
Bits & binary

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 > 0 guard 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.

Example: n = 8

1000 & 0111 = 0000, so 8 passes.

Quick examples

16Power of 2
Reason
2^4 = 16
12Not
Reason
Between 8 and 16
1Power of 2
Reason
2^0 = 1

Live preview

Uses positive-check plus bitwise rule.

Enter nonnegative integer values.

Live result
Press "Run check" to see verdict.

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

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
1

Check a single number

Fast bitwise check in Python.

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.")
2

Powers of 2 from 1 to 20

Reuse helper and print all powers in range.

python
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

A number of form 2^k for whole k >= 0. Examples: 1, 2, 4, 8, 16.
Yes. 1 = 2^0.
For positive powers of two, binary has one set bit. Subtracting 1 clears that bit and sets lower bits, so AND becomes zero.
Zero and negative numbers are not treated as powers of two in this tutorial.
Yes. Repeatedly divide by 2 while even; end at 1 means power of two.
No. Many even numbers like 6 are not powers of two.

🔄 Input / output examples

Replace the number in example 1 to test quickly.

Input numberTypical line
1616 is a power of 2.
1212 is not a power of 2.
11 is a power of 2.
00 is not a power of 2.

Edge cases

n = 0

Not power of two

Fails n > 0 guard.

Negative

Reject immediately

This tutorial treats negatives as not powers of two.

& vs and

Bitwise and logical ops

Use & inside expression and boolean and for guard.

⏱️ Time and space complexity

ApproachTime (single n)Extra space
Bitwise n & (n - 1)O(1)O(1)
Divide-by-2 loopO(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)) == 0 for fast check.
  • Range listing reuses the same helper cleanly.
Did you know?

Many computing sizes are powers of two, so this check appears in memory alignment, bit masks, and tree/heap questions.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful