Bit Manipulation in Coding Interviews: The Five Tricks That Cover Most Questions
5 views
Bit manipulation questions show up less often than arrays or graphs, but when they appear they tend to separate candidates quickly. The good news is that the topic is small. If you understand a handful of operations and can explain why they work, you can handle most of what interviewers ask.
1. XOR cancels pairs
XOR of a number with itself is zero, and XOR with zero returns the number. That is the whole idea behind "find the single number that appears once while every other number appears twice": XOR everything together and the pairs disappear. A common follow-up is "two numbers appear once." XOR all values to get a ^ b, pick any set bit in that result (x & -x isolates the lowest one), and split the array into two groups by that bit. Each group now has one unique value.
2. x & (x - 1) clears the lowest set bit
Subtracting one flips the lowest set bit and every zero below it, so AND-ing with the original removes exactly that bit. This gives you:
- Power of two check: x > 0 and (x & (x - 1)) == 0.
- Counting set bits (Kernighan's method): loop while x is non-zero, clear the lowest bit, count iterations. It runs in time proportional to the number of ones, not the word size.
3. Masks for get, set, clear, toggle
- Get bit i: (x >> i) & 1
- Set bit i: x | (1 << i)
- Clear bit i: x & ~(1 << i)
- Toggle bit i: x ^ (1 << i)
Say these aloud while coding. Interviewers want to hear that you know which operator does which job, not just see it typed.
4. Bitmasks as sets
When n is small (roughly 20 or less), an integer can represent a subset. Iterating mask from 0 to (1 << n) - 1 enumerates every subset, which is the backbone of "generate all subsets" and of bitmask DP problems such as the traveling-salesman variant or "minimum cost to visit all nodes." Mention the 2^n growth explicitly so the interviewer knows you understand the limit.
5. Language-specific traps
- In Java and C++, right shift on negative numbers matters: >> keeps the sign, >>> (Java) does not.
- Python integers are unbounded, so problems that assume 32-bit overflow (reverse bits, sum of two integers without +) need an explicit mask like 0xFFFFFFFF.
- 1 << 31 overflows a signed 32-bit int in some languages. Use a long or unsigned type when in doubt.
How to practice efficiently
Do one problem per pattern rather than twenty random ones: Single Number, Single Number III, Counting Bits, Reverse Bits, Sum of Two Integers, Subsets, and one bitmask DP problem. After each, write a one-line note on the trick it used. Before an interview, reread those notes instead of re-solving everything.
Explaining under pressure
The hardest part is often explaining why a trick works while the interviewer watches. Draw a short 8-bit example, walk through one iteration, and then generalize. If you tend to blank when the clock is running, Linkjob AI (https://linkjob.ai) provides real-time help during job interviews and technical interviews, which can keep you moving when nerves hit.
Bit manipulation rewards understanding over memorization. Learn the five ideas above, practice explaining them with a tiny example, and these questions become some of the fastest wins in a coding round.