Definition
2^k
Positive integers of form 2 to a power.
Powers of two are numbers like 1, 2, 4, 8, 16… — each is 2^k for some whole k >= 0. In binary they have exactly one set bit. This tutorial covers the classic n & (n - 1) trick, a divide-by-2 loop, a live checker, worked Java examples, edge cases, and complexity.
2^k
Positive integers of form 2 to a power.
Binary clue
Exactly one 1 in the binary form.
O(1) trick
Zero means power of two (if n > 0).
No bits
Halve while even; finish at 1.
Try 16 / 6
See binary and the AND result.
6 fails
Even ≠ power of two.
A power of two is a positive integer of the form 2^k. So 16 passes (2^4) while 6 fails even though it is even.
The interview favorite is n > 0 && (n & (n - 1)) == 0. Subtracting 1 from a power of two flips that single set bit and fills lower bits with ones, so AND becomes zero.
It is a classic bit-trick interview question that connects binary intuition to real systems (buffers, heaps, masks).
1, 2, 4, 8, 16…
Binary has a single 1.
Zero and negatives fail.
Bitwise test is constant time.
In short: positive with exactly one set bit — use n > 0 && (n & (n - 1)) == 0.
Given an integer n, decide whether it is a power of two (2^k for some k >= 0).
// 16 -> 10000 binary -> one set bit -> yes
// 8 -> 1000 -> yes
// 6 -> 110 -> two bits -> no
// 1 -> 1 -> 2^0 -> yes | Item | Type | Description |
|---|---|---|
n / num | int | Value to test (must be > 0 for yes). |
| Return | boolean | true when n is 2^k. |
| Key expression | bitwise | (n & (n - 1)) == 0 with n > 0. |
function is_power_of_two_bitwise(n):
if n <= 0:
return false
return (n & (n - 1)) == 0
function is_power_of_two_loop(n):
if n <= 0:
return false
while n > 1 and n mod 2 == 0:
n = n / 2
return n == 1 | Method | Idea | Notes |
|---|---|---|
| n & (n - 1) | One set bit clears to zero | Interview favorite — O(1) |
| Divide by 2 | Halve while even; end at 1 | No bitwise syntax needed |
Integer.bitCount | Count set bits == 1 | Readable; Integer.toBinaryString also helps |
| Goal | Pattern |
|---|---|
| Classic check | n > 0 && (n & (n - 1)) == 0 |
| Alt style | n != 0 && (n & (n - 1)) == 0 (still guard n > 0) |
| Reject zero | if (n <= 0) return false; |
| Loop check | while (n > 1 && n % 2 == 0) n /= 2; |
| Loop success | return n == 1; |
| Interview phrase | Positive with exactly one set bit |
Same question — different tools and traps.
n & (n - 1)Fastest interview answer
n //= 2Great without bitwise ops
n % 2 == 0Not enough — 6 fails
1000 & 0111AND is 0 — yes
Reach for a power-of-two check whenever sizes or masks must be exact powers.
Classic “one set bit” prompt.
Many systems prefer power-of-two lengths.
Capacity and height reasoning.
Confirm a mask is a single bit.
Use modulo if you only care about parity.
Key benefit: one O(1) expression that proves you understand binary, not just even/odd.
Uses positive-check plus the bitwise rule, and shows binary for small n.
Three complete Java programs — bitwise check for 16, list powers from 1 to 20, and a divide-by-2 loop without bitwise operators. Click View Output to reveal sample console results.
The classic one-liner interview answer.
Fast bitwise check in Java.
public class IsPowerOfTwo {
static boolean isPowerOfTwo(int num) {
return num > 0 && (num & (num - 1)) == 0;
}
public static void main(String[] args) {
int number = 16;
if (isPowerOfTwo(number)) {
System.out.println(number + " is a power of 2.");
} else {
System.out.println(number + " is not a power of 2.");
}
}
} 16 in binary is 10000. Then 15 is 01111, so AND is zero. Combined with num > 0, the helper returns true.
Reuse the helper to list nearby powers of two.
Reuse the helper method and print all powers in range.
public class Powers1To20 {
static boolean isPowerOfTwo(int num) {
return num > 0 && (num & (num - 1)) == 0;
}
public static void main(String[] args) {
System.out.println("Power of 2 in the range 1 to 20:");
for (int i = 1; i <= 20; i++) {
if (isPowerOfTwo(i)) {
System.out.print(i + " ");
}
}
}
} Within 1..20 the powers are 1, 2, 4, 8, and 16. Numbers like 6, 10, 12, and 14 are even but fail the bit test.
Halve while even; finish at 1 means power of two.
public class PowerOfTwoLoop {
static boolean isPowerOfTwo(int num) {
if (num <= 0) {
return false;
}
while (num > 1 && num % 2 == 0) {
num /= 2;
}
return num == 1;
}
public static void main(String[] args) {
int[] values = { 1, 8, 6, 16, 0 };
for (int value : values) {
String label = isPowerOfTwo(value) ? "yes" : "no";
System.out.println(value + ": " + label);
}
}
} 8 becomes 4, then 2, then 1 — success. 6 becomes 3 and stops because 3 is odd and not 1.
Zero and negatives are not powers of two here.
Clears the lowest set bit of n.
Zero means exactly one set bit was present.
true for 2^k, else false.
Compare the bitwise rule on a yes case and a no case.
| n | Binary | n - 1 | n & (n - 1) | Verdict |
|---|---|---|---|---|
8 | 1000 | 0111 | 0000 | Yes |
16 | 10000 | 01111 | 00000 | Yes |
6 | 110 | 101 | 100 | No |
1 | 1 | 0 | 0 | Yes (2^0) |
One set bit clears to zero under AND; multiple set bits leave leftovers.
Where power-of-two checks show up beyond the interview prompt.
Bit tricks and binary intuition.
Example: is_pow2(16).
Validate buffer or array sizes.
Example: size must be 2^k.
Find powers inside a band.
Example: 1..20 list.
Show one-set-bit intuition.
Example: 8 vs 6 table.
Same answer without &.
Example: Example 3.
Continue the interview chain.
Example: related CTA.
Pro Tip: open with “positive integer with exactly one set bit” before writing the expression.
Why these approaches work well for beginners and interviews.
The bitwise check is O(1) and tiny.
Forces you to picture set bits clearly.
Same answer without needing & syntax.
Trace 8 and 6 on paper in seconds.
Pro Tip: lead with the bit trick; offer the divide loop if the interviewer bans bitwise ops.
Small habits that keep power-of-two solutions interview-ready.
Never skip the positive check.
Bitwise & inside; boolean && for the guard.
One yes and one no seals understanding.
Useful if bitwise operators are disallowed.
“Exactly one set bit” shows intent.
Pro Tip: sanity-check 1, 16, 6, and 0 — if those four behave, your logic is solid.
Mistakes that commonly break power-of-two programs.
0 & (-1) can look tricky in languages with wraparound.
→ Require n > 0 explicitly.
Boolean && does not clear bits; it short-circuits on truthiness.
→ Use bitwise & inside the expression.
6, 10, 12 pass even but fail here.
→ Need exactly one set bit.
1 = 2^0 is a power of two.
→ Include 1 in yes cases.
This tutorial treats negatives as no.
→ Reject n <= 0.
Handle these before claiming the check is complete.
One set bit still counts.
n > 0 fails immediately.
Two set bits in binary.
Return false here.
Use & for bits; && for the positive guard.
10000 & 01111 = 0.
Handy follow-ups interviewers sometimes ask.
Try these variations to lock in the pattern.
n > 0 && (n & (n - 1)) == 0.num != 0 && (num & (num - 1)) == 0. Interview phrase: positive integer with exactly one set bit.Quick Takeaway: positive with exactly one set bit — n > 0 && (n & (n - 1)) == 0.
| Approach | Time | Extra space |
|---|---|---|
| Bitwise n & (n - 1) | O(1) | O(1) |
| Divide-by-2 loop | O(log n) | O(1) |
| Range 1..U scan | O(U) checks | O(1) |
Prefer the bitwise check in interviews unless asked to avoid bit operators.
A power of two is a positive integer with exactly one set bit. Use n > 0 && (n & (n - 1)) == 0, or fall back to dividing by 2 until you reach 1.
Practice the three examples above, then continue to checking cube numbers.
One set bit + n > 0 means power of two; even alone is not enough.
&& for bit clearingDecide 2^k the interview-friendly way.
one set bit
Definitionn & (n-1)
Bitwisen > 0
Edgeshalve to 1
FallbackO(1) bits
AnalysisMany computing sizes are powers of two, so this check appears in memory alignment, bit masks, and tree/heap questions.
Learn how to check whether a number is a perfect cube in Java.
8 people found this page helpful