Java Bit Manipulation Program · OT30
Check If a Number Is a Power of Two in Java
A power of two has exactly one 1 bit. Subtracting 1 turns that bit off and all lower bits on, so n & (n - 1) is 0 only for powers of two.
Problem Statement
Return true if an integer is a power of two: 1, 2, 4, 8, …
0 and negative numbers are never powers of two, including Integer.MIN_VALUE, whose bit pattern has a single 1 bit.
Input/Output Examples
64OutputtrueExplanation: 26 = 64.96OutputfalseExplanation: 96 = 64 + 32.0, -8 and Integer.MIN_VALUEOutputfalseExplanation: powers of two are positive.Visual Explanation
| value | binary | |
|---|---|---|
| n | 64 | 1000000 |
| n - 1 | 63 | 0111111 |
| n & (n - 1) | 0 | 0000000 |
The n > 0 check is required: 0 also gives 0 for the AND, and Integer.MIN_VALUE has one 1 bit but is negative.
Approaches
The bit trick answers in constant time. The division loop is the straightforward alternative.
Approach 1: Bit Trick (Recommended)
Idea: return n > 0 && (n & (n - 1)) == 0.
- Reject zero and negative numbers.
- Check that clearing the lowest 1 bit leaves 0.
Time: O(1) · Space: O(1).
Approach 2: Divide by 2
Idea: keep halving while the number is even; a power of two ends at exactly 1.
- Reject
n ≤ 0. - While
nis even, divide by 2. - Return whether
n == 1.
Time: O(log n) · Space: O(1).
Java Solutions
Each tab is a complete program that runs on Java 17 or later. Both programs print the same output for the same inputs.
Approach 1: Bit Trick (Recommended)
One comparison and one AND, with no loop.
public class PowerOfTwoBit {
// A power of two has exactly one 1 bit; n & (n - 1) removes it and leaves 0
static boolean isPowerOfTwo(int n) {
return n > 0 && (n & (n - 1)) == 0;
}
public static void main(String[] args) {
int[] inputs = {64, 1, 0, 96, -8, Integer.MIN_VALUE};
for (int n : inputs) {
System.out.println(n + " -> " + isPowerOfTwo(n));
}
}
}
64 -> true 1 -> true 0 -> false 96 -> false -8 -> false -2147483648 -> false
Time complexity: O(1) · Space complexity: O(1).
Approach 2: Divide by 2
Easy to explain without any knowledge of binary representation.
public class PowerOfTwoDivide {
// Keep dividing by 2 while the number is even; a power of two ends at 1
static boolean isPowerOfTwo(int n) {
if (n <= 0) {
return false;
}
while (n % 2 == 0) {
n /= 2;
}
return n == 1;
}
public static void main(String[] args) {
int[] inputs = {64, 1, 0, 96, -8, Integer.MIN_VALUE};
for (int n : inputs) {
System.out.println(n + " -> " + isPowerOfTwo(n));
}
}
}
64 -> true 1 -> true 0 -> false 96 -> false -8 -> false -2147483648 -> false
Time complexity: O(log n) · Space complexity: O(1).
Complexity Analysis
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Bit Trick | O(1) | O(1) |
| Divide by 2 | O(log n) | O(1) |
The bit trick is constant time. The division loop runs at most 31 times for an int, so it is also bounded, but it does more work.
Interview Tips and Recommended Approach
- Check
n > 0first. It handles 0, negatives andInteger.MIN_VALUE. - Know
Integer.bitCount(n) == 1as a readable alternative for positiven. - Extend the idea. Powers of four are powers of two whose bit sits in an even position.
- Know where it is used: hash table sizes, buffer sizes and alignment checks.
Recommended Approach: Bit Trick
Why: it answers in constant time with one expression, and it is the classic demonstration of n & (n - 1).