Power of Two
An easy Bit Manipulation problem included in Love Babbar 450, Striver A2Z. Below: the roles whose interviews prioritise this topic, and how to practise it.
- Topic
- Bit Manipulation
- Sheets
- 2
- Core for
- 9 roles
- Platform
- LeetCode
The problem
Given an integer n, return true if it is a power of two, and false otherwise. An integer is a power of two if there exists an integer x such that n == 2^x.
Example 1
- Input
- n = 1
- Output
- true
- Why
- 2^0 = 1, so 1 is a power of two.
Example 2
- Input
- n = 16
- Output
- true
- Why
- 2^4 = 16, so 16 is a power of two.
Example 3
- Input
- n = 3
- Output
- false
- Why
- There is no integer x such that 2^x = 3.
Example 4
- Input
- n = 0
- Output
- false
- Why
- 0 is not a power of two since 2^x is always positive for any real x.
Constraints
- -2^31 <= n <= 2^31 - 1
How to think about it
Updated 2026-09-09In binary, a power of two is a solitary set bit standing above a sea of zeroes. Decrementing that number by one turns that single set bit off and flips all subsequent zeroes to ones. Consequently, a strictly positive number and its predecessor share no set bits in common if and only if it is a power of two.
Approaches, worst first
Iterative division or shift
time O(log n) · space O(1)
Repeatedly divide n by two while ensuring remainder is zero, or right-shift until n becomes one. Works fine, but requires up to thirty-one loop checks.
Population count verification
time O(1) · space O(1)
Count the number of 1-bits in n; if the count equals one and n is positive, return true. Reliable, but still scans bit positions or calls helper primitives.
Bitwise predecessor maskWrite this one
time O(1) · space O(1)
Evaluate `n > 0 && (n & (n - 1)) === 0`. If n has only one set bit, `n - 1` inverts all bits from that position downward, yielding zero under bitwise AND in a single instruction.
Where people lose marks · 3
- Omitting the `n > 0` check causes 0 to pass `0 & -1 === 0` and claim zero is a power of two.
- Negative numbers like -2^31 have a single 1-bit in two's complement sign position; forgetting the positivity check falsely identifies them as powers of two.
- Neglecting operator precedence: `n & n - 1 === 0` binds subtraction and equality before bitwise AND, altering the intended logic entirely.
The theory behind it
Bit Manipulation — the ground this problem stands on. All Bit Manipulation problems
What Bit Manipulation is
Bit manipulation is the practice of working directly on the individual ones and zeros that form numbers in computer memory. Every integer is stored as a tiny row of electrical switches that are either on or off. Instead of running arithmetic loops, bitwise operations flip, mask, shift, or combine these switches in a single hardware cycle. This allows compact storage of sets and blazing-fast checks without allocating extra memory.
When to reach for it
Reach for bit manipulation when problems ask to find a unique non-duplicate number, count set bits, determine if a value is a power of two, or pack a set of small booleans into a single integer. Prompts mentioning constant O(1) auxiliary space constraints on array queries often hint at XOR cancellation. It is also the foundation of bitmask dynamic programming, where subsets of up to twenty items are tracked as integer masks.
How the pattern works
Think of an integer as a fixed-length set of flags. Use bitwise AND to inspect if a specific bit is set, bitwise OR to turn a bit on, and bitwise XOR to flip a bit or cancel out matched pairs. Learn the standard bit tricks: n AND with n minus one clears the lowest set bit, which counts ones quickly, while n AND with negative n isolates the lowest set bit. When packing sets into masks, represent the empty set as zero and add item i by shifting one left by i and combining with OR.
What each operation costs
| Operation | Time |
|---|---|
| bitwise operation like AND, OR, or XOR | O(1) |
| count set bits across fixed integer width | O(1) |
| find single non-duplicate number using XOR | O(n) |
What usually goes wrong with Bit Manipulation
- Forgetting that bitwise operators have lower operator precedence than equality and arithmetic comparisons in most languages, evaluating expressions in the wrong order without parentheses.
- Using signed right shift instead of unsigned logical right shift when processing negative numbers, which fills high-order bits with ones instead of zeros.
- Shifting bits by thirty-two or more on standard 32-bit integers, causing undefined behavior or wrapped bit shifts that yield incorrect masks.
Which roles need this problem
Bit Manipulation is a core topic for these 9 roles — if you're targeting one of them, this problem is early in your path, not optional.
Secondary for 2 more roles, including ML Engineer, Information Retrieval Engineer.
Track this in your role's order
Pick your target role and all 370 problems — including this one — resequence to what that interview actually asks. Free.
Start freeMore Bit Manipulation problems
Problem set and role mapping as of .