Check Whether a Number Is a Power of Two
Decide whether a positive number is an exact power of two in constant time.
A positive integer is a power of two when its binary form has exactly one set bit. The trick n & (n - 1) clears the lowest set bit, so for a power of two the result is 0. Return n > 0 && (n & (n - 1)) == 0. For 16 (10000) this is 16 & 15 = 0, so 16 is a power of two.
Problem Statement
Given an integer n, decide whether it is an exact power of two — that is,
n = 2^k for some k >= 0 (so 1, 2, 4, 8, 16, ...). Return true or
false. Zero and negative numbers are not powers of two.
For example, 16 is 2^4, so it is a power of two, while 18 is not.
Input: A single integer n.
Output: A boolean: true if n is a power of two, otherwise false.
Examples
Input: 16
Output: true16 in binary is 10000, a single set bit, so it is 2^4.
Input: 18
Output: false18 in binary is 10010, two set bits, so it is not a power of two.
Constraints
n fits in a 32-bit intn must be strictly positive to qualify
Think Before You Code
Reveal the questions to ask yourself first
- How many 1-bits does a power of two have in binary?
- What does subtracting 1 from a power of two do to its bits?
- Why must you reject zero and negative values up front?
Hints
Open them one at a time — try after each before revealing the next.
Hint 1
Hint 2
Hint 3
Approach
Reveal the step-by-step approach
Use the classic bit-clearing identity.
- Reject non-positive
nimmediately (0 and negatives are never powers of two). - Compute
n & (n - 1). Subtracting 1 from a power of two clears its only set bit and sets all lower bits, so the AND wipes out to 0. - Return
n > 0 && (n & (n - 1)) == 0.
If you prefer no bit tricks, you can instead divide by 2 while the number is even and check that you reach exactly 1.
Dry Run
Walk through the example step by step
Checking n = 16:
n = 16 = 10000 (binary)
n - 1 = 15 = 01111 (binary)
n & (n-1) = 10000 & 01111 = 00000 = 0
n > 0 and result == 0 -> true
Solution
Reveal the full Java solution
public class PowerOfTwo {
public static boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
public static void main(String[] args) {
System.out.println(isPowerOfTwo(16)); // true
System.out.println(isPowerOfTwo(18)); // false
}
}
A power of two has exactly one bit set. Subtracting 1 turns that bit off and
all the lower bits on, so n & (n - 1) has no overlapping bits and equals 0.
The n > 0 guard is essential: without it, 0 & -1 is also 0 and would wrongly
report zero as a power of two.
O(1)Space: O(1)Common Mistakes
- Omitting the `n > 0` guard, so 0 is misreported as a power of two.
- Applying the bit trick to negative numbers, whose two's-complement form breaks it.
Edge Cases to Test
- n = 1 is 2^0, a power of two.
- n = 0 and negative numbers must return false.
Interview Follow-Ups
- How would you solve this with a loop that repeatedly divides by 2 instead?
- How would you find the exponent k such that n == 2^k?
Practising for Java interviews?
CodeBegun's Java Full Stack with AI program builds this problem-solving muscle with mentor review and mock interviews.
Explore the Java Full Stack program →