Number Theory
Mathematics
Programming
Algorithms
Binary Representation

How to check if a number is a power of 2

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Introduction

A positive integer is a power of two if its binary representation contains exactly one set bit. That property makes the check both elegant and very fast in code.

The most common solution is a bit trick: for positive n, the expression n & (n - 1) is zero only when n is a power of two. Understanding why that works is more valuable than memorizing it blindly.

The Bitwise Rule

Powers of two look like this in binary:

  • '1 is 0001'
  • '2 is 0010'
  • '4 is 0100'
  • '8 is 1000'

Each has exactly one 1 bit.

If you subtract 1, that bit flips to 0, and all lower bits become 1.

Example with 8:

text
8      = 1000
8 - 1  = 0111
AND    = 0000

That gives the classic check:

python
1def is_power_of_two(n: int) -> bool:
2    return n > 0 and (n & (n - 1)) == 0
3
4print(is_power_of_two(8))
5print(is_power_of_two(10))

Why n > 0 Matters

The positivity check is not optional.

python
print(0 & (0 - 1))

Without n > 0, zero would accidentally pass the bitwise part of the test even though zero is not a power of two.

Negative numbers are also not powers of two in the usual integer sense for this problem, so the guard belongs in the final condition.

Mathematical Alternatives

You can also use logarithms or repeated division, but those approaches are usually less attractive.

Repeated division version:

python
1def is_power_of_two_division(n: int) -> bool:
2    if n <= 0:
3        return False
4    while n % 2 == 0:
5        n //= 2
6    return n == 1

This is easy to understand, but slower and more verbose than the bitwise form.

A logarithm-based approach can work too, but floating-point math introduces unnecessary precision concerns for a problem that can be solved exactly with integer operations.

Examples in Different Languages

Python:

python
def is_power_of_two(n: int) -> bool:
    return n > 0 and (n & (n - 1)) == 0

Java:

java
public static boolean isPowerOfTwo(int n) {
    return n > 0 && (n & (n - 1)) == 0;
}

JavaScript:

javascript
function isPowerOfTwo(n) {
  return Number.isInteger(n) && n > 0 && (n & (n - 1)) === 0;
}

For JavaScript, remember that bitwise operators work on 32-bit integers, so if you care about very large integers, you need a different approach or BigInt.

Where This Check Is Useful

This pattern shows up in systems and algorithm work because powers of two appear naturally in:

  • memory sizing
  • buffer capacities
  • binary tree and heap logic
  • bitmask design
  • low-level performance optimizations

Even if the direct problem seems academic, the underlying representation idea is widely useful.

Common Pitfalls

The biggest mistake is forgetting the n > 0 guard. The bit trick alone is not enough.

Another common issue is applying the bitwise solution to floating-point or non-integer values without validating the input first.

People also sometimes use logarithms for elegance and then run into precision edge cases. Integer arithmetic is the better tool here.

Finally, in JavaScript, be careful with large values because normal bitwise operators are limited to 32-bit integer behavior.

Summary

  • A power of two has exactly one set bit in binary.
  • The fast check is n > 0 and (n & (n - 1)) == 0.
  • Zero and negative numbers should return false.
  • Bitwise checking is exact and usually better than logarithms for this problem.
  • The same idea works cleanly in Python, Java, and many other languages.
  • Understanding the binary pattern is more important than memorizing the expression.

Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.