Bit Manipulation Problems — Signed Shift Traps & XOR Tricks
Arithmetic shift on negative numbers caused infinite loops in production.
- XOR cancels pairs: A^A=0, A^0=A — finds the single unique element in O(n) time and O(1) space
- n & (n-1) clears the rightmost set bit — used for counting bits and power-of-two checks
- Isolate rightmost set bit with n & -n — foundational for Fenwick trees and bitmask grouping
- Largest performance win: loops replaced by constant-time bitwise ops; up to 10x faster than arithmetic alternatives
- Biggest production pitfall: operator precedence —
n & 1 == 0is parsed asn & (1 == 0); always parenthesize - Signed vs unsigned shifts: Java's >> preserves sign, >>> is logical; wrong choice breaks bit-counting on negative numbers
Imagine every number is secretly a row of light switches — each switch is either ON (1) or OFF (0). Bit manipulation is just flipping, checking, or combining those switches directly, without doing normal math. It's like adjusting Christmas lights one bulb at a time instead of replacing the whole string. Computers think in exactly this language, so when you work at the switch level, you skip all the middleman work and get blazing-fast results.
Every major tech company — Google, Meta, Amazon — consistently includes at least one bit manipulation problem in their coding rounds. That's not a coincidence. These problems test whether you understand how a CPU actually works at its lowest level, separating candidates who memorized patterns from those who genuinely understand computation. A single XOR or a bitwise AND can replace entire loops, cut memory usage in half, and run in constant time — and interviewers know it.
The core challenge with bit manipulation isn't the operators themselves — you likely know & | ^ ~ << >>. The challenge is pattern recognition: seeing a problem about finding a missing number or detecting duplicates and immediately knowing 'this is an XOR problem.' Without that pattern library wired into your brain, you'll stare at the problem and reach for a HashSet when a two-line XOR solution was sitting right there.
By the end of this article you'll have five rock-solid bit manipulation patterns locked in with real runnable code, you'll understand exactly why each trick works at the binary level (not just that it works), and you'll know the specific gotchas that cause smart engineers to get these wrong under interview pressure — things like signed integer overflow, arithmetic vs. logical right shifts, and why Brian Kernighan's bit-counting trick is faster than a naive loop.
The Power of XOR: Solving 'Single Number' (LeetCode 136)
The XOR (exclusive OR) operator is the 'magic wand' of bit manipulation. Its properties are unique: any number XORed with itself is 0 ($A \oplus A = 0$), and any number XORed with 0 is itself ($A \oplus 0 = A$). It is also commutative and associative.
In an interview, if you see a problem where every element appears twice except for one, don't reach for a Map or a Set. That's $O(n)$ space. By XORing every number in the array together, all pairs cancel each other out, leaving only the unique element. This is the gold standard for efficiency: $O(n)$ time and $O(1)$ space.
Counting Set Bits: Brian Kernighan’s Algorithm (LeetCode 191)
Interviewers often ask for the 'Hamming Weight' or the number of set bits (1s) in an integer. A naive approach checks every bit one by one ($O(32)$). However, Brian Kernighan’s algorithm is much faster because it only iterates as many times as there are set bits.
The trick lies in the expression n & (n - 1). This operation always flips the least significant (rightmost) set bit to zero. By repeating this until the number becomes zero, you count the bits in record time.
(n & (n - 1)) == 0 (and $n > 0$), it's a power of two because powers of two have exactly one set bit.>>> when shifting negative or unknown inputs in bit-counting loops.(n > 0) && ((n & (n - 1)) == 0).Power of Two Check: Beyond the Obvious
A number is a power of two if it has exactly one bit set in its binary representation. The expression n & (n - 1) == 0 directly verifies that. But there's a subtlety: 0 also satisfies (0 & -1) == 0, so you must include the n > 0 guard.
Beyond the standard check, interviewers may ask variations: "Is n a power of 4?" — here you combine the power-of-two check with a bit mask that isolates the parity of the set bit position: (n & 0x55555555) != 0 for 32-bit integers.
- 1000 (8) has one bit; 1010 (10) has two bits.
- Subtracting 1 flips the trailing zeros to ones: 1000 - 1 = 0111.
- AND of these two values always gives zero for powers of two.
- For non-powers, at least one matching 1-bit remains after subtraction.
n > 0 before the bit trick; zero is not a power of two but passes the mask.(n > 0) && ((n & (n - 1)) == 0).(n & 0x55555555) != 0.Isolating the Rightmost Set Bit: n & -n
The expression n & -n isolates the lowest set bit of an integer. How does it work? Two's complement negation inverts all bits and adds one. So -n = ~n + 1. ANDing with n zeroes out everything except the rightmost 1-bit. This is crucial for Fenwick trees (Binary Indexed Trees) and for grouping elements based on the last differing bit.
Example: n = 12 (1100), -n = -12 (0100 in two's complement, limited to lower bits). n & -n = 0100 = 4, which is the value of the lowest set bit.
i += i & -i.i & -i on a signed integer with value 0 leads to division by zero or infinite loop.n & -n.Bitmasking: Enumerate All Subsets in O(2^n)
For problems like "generate all subsets" (LeetCode 78), a bitmask represents which elements are included. Each integer from 0 to (1 << n) - 1 maps to a unique subset. The j-th bit being 1 means the j-th element is in the subset. This is an $O(2^n * n)$ approach (or $O(2^n)$ if you process each subset directly).
The bitmask method is impressively compact: one loop over mask and inner loop over bits. Interviewers love it because it shows you understand binary representation and space efficiency — no recursion overhead.
1 << n overflows the int sign bit. Use 1L << n and cast to int if needed, or use BigInteger for n > 62. Always check n against 30 before using int masks.1 << n overflow when n >= 31.Lost Log Entries from a Signed Shift in a Bit Counter
n >>= 1 to shift right. For negative numbers, the arithmetic shift preserves the sign bit, so n never becomes zero — infinite loop. A naive loop with >>> would have terminated correctly.n >>= 1 with n >>>= 1 (logical shift) in the bit-counting function. Added a unit test with a negative input to catch regressions.- Always use >>> (logical right shift) when iterating over bits in Java unless you explicitly need sign extension.
- Test bit-manipulation code with edge cases: negative numbers, zero, and the largest positive value (Integer.MAX_VALUE).
- Code reviews for performance-critical bit logic should include a partner who understands signedness.
n > 0 && (n & (n - 1)) == 0. Zero has no set bits but isn't a power of two.1L << position to avoid integer overflow. In Java, 1 << 31 is negative; use long for masks beyond 31 bits.Key takeaways
n & (n - 1) is a powerhouse—it flips the rightmost 1-bit to 0. It is the core of Brian Kernighan’s counting algorithm and power-of-two tests.>>> or >> operator.n & -n1 << n when n >= 31. Use 1L << n for large sets.Common mistakes to avoid
4 patternsForgetting operator precedence
n & 1 == 0 returns boolean but then bitwise ANDs with n, causing unexpected results. In Java, == has higher precedence than &.(n & 1) == 0. Also check similar issues with shift operators: (n >> i) & 1 is safe but n & 1 << i needs parentheses: n & (1 << i).Ignoring signedness when right-shifting
>> is arithmetic (preserves sign), >>> is logical (always fills with zero).>>> for shifting bits in count loops. If you need to treat an int as unsigned, use Integer.parseUnsignedInt or work with >>> throughout.Integer overflow with large left shifts
1 << 31 produces -2147483648 because the bit shifts into the sign bit. 1 << 32 on a 32-bit int is undefined (actually shift distance mod 32).1L << position for positions beyond 30. If you need 64 bits, work with long. For arbitrary shifts, consider BigInteger or handle overflow explicitly.Assuming XOR solves all 'appears twice' variants
Interview Questions on This Topic
LeetCode 136: Single Number — Given a non-empty array of integers, every element appears twice except for one. Find that single one in O(1) space.
result = 0. For each number n, result ^= n. All pairs cancel out, leaving the unique element. Time: O(n), space: O(1).Frequently Asked Questions
That's Coding Patterns. Mark it forged?
3 min read · try the examples if you haven't