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.
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:
- '
1is0001' - '
2is0010' - '
4is0100' - '
8is1000'
Each has exactly one 1 bit.
If you subtract 1, that bit flips to 0, and all lower bits become 1.
Example with 8:
That gives the classic check:
Why n > 0 Matters
The positivity check is not optional.
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:
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:
Java:
JavaScript:
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
- How to check if an integer is a power of 3?
- How to check if two permutations are symmetric?
- How to check if two words are anagrams
- How to check/find if an item is in a DEQUE
- How to check if a point is inside an ellipsoid?
- How to check if a point lies on a line between 2 other points
- How to checkout old git commit including all submodules recursively?
- How to code the maximum set packing algorithm?

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 courseTrack 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.