Bit Manipulation — Complete Interview Guide for FAANG Engineers
Advertisement
Why Bit Manipulation?
Bitwise operations work directly on binary representations, enabling O(1) tricks that would otherwise need loops or extra space. Bit manipulation problems appear at Google, Amazon, Meta, and Microsoft — interviewers use them to test depth of systems thinking and ability to reason at the bit level.
Essential Bit Operations
| Operation | Expression | Meaning |
|---|---|---|
| Check bit i | (n >> i) & 1 | Is bit i set? |
| Set bit i | n | (1 << i) | Set bit i to 1 |
| Clear bit i | n & ~(1 << i) | Set bit i to 0 |
| Toggle bit i | n ^ (1 << i) | Flip bit i |
| Remove last set bit | n & (n-1) | Clear rightmost 1 |
| Isolate last set bit | n & (-n) | Keep only rightmost 1 |
| Check power of 2 | n > 0 and (n & (n-1)) == 0 | Exactly one bit set |
| Count set bits | bin(n).count('1') | Popcount |
| XOR self = 0 | n ^ n = 0 | Cancel duplicates |
| XOR with 0 | n ^ 0 = n | Identity element |
The 7 Core Patterns
Pattern 1 — XOR for Duplicate Cancellation
XOR of a number with itself = 0. XOR with 0 = identity. Use to find single or missing numbers.
result = 0
for n in nums:
result ^= n # all duplicates cancel; single element survivesPattern 2 — Brian Kernighan (Count Set Bits)
n & (n-1) removes the lowest set bit. Count iterations until n reaches 0.
count = 0
while n:
n &= n - 1 # drop the lowest set bit
count += 1Pattern 3 — Bitmask DP (Subsets as State)
State = bitmask of items selected. Iterate over all 2^n masks.
for mask in range(1 << n):
for i in range(n):
if mask & (1 << i):
pass # item i is in this subsetPattern 4 — Bitmask Enumeration (Subsets of a Mask)
Enumerate all subsets of a bitmask in O(2^popcount) using sub = (sub-1) & mask.
sub = mask
while sub:
process(sub)
sub = (sub - 1) & maskPattern 5 — Two's Complement Tricks
Negative numbers in two's complement: -n = ~n + 1.
n & (-n) isolates the rightmost set bit. Used in BIT (Fenwick Tree) traversal.
Pattern 6 — Bit by Bit Construction
Build answer bit by bit from MSB to LSB. Common with XOR prefix sums in a trie.
prefix_xor = 0
for n in nums:
prefix_xor ^= n
# query trie for max XOR with prefix_xorPattern 7 — Bit Reversal / Rotation
Reverse bits of a 32-bit integer by extracting and shifting one bit at a time.
result = 0
for _ in range(32):
result = (result << 1) | (n & 1)
n >>= 1Language-Specific Bit Operations
Python
n & m # AND
n | m # OR
n ^ m # XOR
~n # NOT (bitwise complement)
n << k # left shift
n >> k # right shift (arithmetic)
bin(n).count('1') # popcountC/C++
n & m; n | m; n ^ m; ~n; n << k; n >> k;
__builtin_popcount(n); /* GCC popcount */
__builtin_clz(n); /* count leading zeros */
__builtin_ctz(n); /* count trailing zeros */Java
n & m; n | m; n ^ m; ~n; n << k; n >> k; n >>> k; // unsigned right shift
Integer.bitCount(n);
Integer.highestOneBit(n);
Integer.numberOfLeadingZeros(n);JavaScript
n & m; n | m; n ^ m; ~n; n << k; n >> k; n >>> k;
n.toString(2).split('').filter(x => x === '1').length; // popcountComplexity Summary
| Pattern | Time | Space | Example Problem |
|---|---|---|---|
| XOR cancellation | O(n) | O(1) | Single Number (LC 136) |
| Brian Kernighan | O(log n) | O(1) | Number of 1 Bits (LC 191) |
| Bitmask DP | O(2^n * n) | O(2^n) | Partition K Subsets (LC 698) |
| Subset enumeration | O(3^n) | O(1) | All subset queries |
| Bit by bit (trie) | O(32n) | O(32n) | Max XOR (LC 421) |
Common Pitfalls
- Overflow with
1 << 31in C++/Java. Use1u << 31(unsigned) or1L << 31(long) to avoid signed integer overflow. - Python
~nis not2^32 - 1 - n. Python integers are arbitrary precision;~n = -(n+1)always. n & (n-1)on n=0 yields 0, not undefined. But calling it in a loop without a guard causes an infinite loop.- Forgetting bitmask DP needs
4*ntime and2^nspace. Feasible only for n <= 20. - Arithmetic vs logical right shift. Python
>>is arithmetic (preserves sign). JavaScript>>>is logical (zero-fills).
Problem Index
| # | Problem | Pattern | Difficulty |
|---|---|---|---|
| 01 | Single Number | XOR cancel | Easy |
| 02 | Single Number II | Bit counting 3-state | Medium |
| 03 | Single Number III | XOR + partition | Medium |
| 04 | Number of 1 Bits | Brian Kernighan | Easy |
| 05 | Counting Bits | DP with bits | Easy |
| 06 | Reverse Bits | Bit-by-bit | Easy |
| 07 | Missing Number | XOR or math | Easy |
| 08 | Find the Difference | XOR | Easy |
| 09 | Power of Two | n and (n-1) == 0 | Easy |
| 10 | Sum of Two Integers | Bit addition | Medium |
| 11 | Maximum XOR of Two Numbers | Binary Trie | Medium |
| 12 | Subsets | Bitmask enumerate | Medium |
| 13 | Total Hamming Distance | Bit position analysis | Medium |
| 14 | Bitwise AND of Numbers Range | Common prefix | Medium |
| 15 | Divide Two Integers | Bit shifting | Medium |
| 16 | Minimum XOR Sum | Bitmask DP | Hard |
| 17 | Partition to K Equal Sum Subsets | Bitmask DP | Medium |
| 18 | Bit Manipulation Master Recap | Cheatsheet | — |
Key Takeaways
- Two XOR identities drive most bit tricks:
a ^ a = 0(cancellation) anda ^ 0 = a(identity). n & (n-1)removes the lowest set bit — Brian Kernighan's algorithm counts set bits in O(popcount) iterations.n & (-n)isolates the lowest set bit — foundational for Fenwick Trees and bitmask subset enumeration.- Bitmask DP is feasible for n up to 20; it encodes which items are selected as a single integer state.
ceil(log2(k))bitmasks are needed to cancel elements appearing k times — XOR (k=2) uses 1, mod-3 (k=3) uses 2.- Language-specific overflow rules differ: Python has arbitrary-precision integers; C++/Java need explicit casts for bit 31.
- Bit manipulation often converts O(n) space solutions to O(1) — the space constraint in a problem is your signal to think bitwise.
Advertisement