O(1) low-level bitwise operations, masks, and XOR parity tricks
238 problems·45 Easy·110 Medium·83 Hard
Pattern Study Guide & Cheat Sheet· O(1) or O(number of set bits) · O(1) strictly constant memory
▼
Perform bitwise operations (&, |, ^, ~, <<, >>) to check, set, toggle, or count bits in O(1) time and space without allocating memory.
Core Invariant: Essential Bit Identities: 1) `x ^ x = 0` and `x ^ 0 = x` (cancels pairs); 2) `n & (n - 1)` clears the lowest set bit (Brian Kernighan); 3) `n & -n` isolates lowest set bit; 4) `(1 << k)` creates mask for k-th bit.
Recognize it (Keywords & Signals)
Single number (every element appears twice except one)
Number of 1 bits (Hamming weight)
Counting bits from 0 to N
Subsets represented as bitmasks (N <= 20)
Power of two verification
When NOT to use
Numbers exceed 64-bit integer limits and bitwise representations become complex strings, or problem requires decimal digits.
How to solve (Step-by-step)
1.Identify if problem involves duplicate cancellation (XOR), bit counting (`n & (n - 1)`), or subset mask (`1 << n`).
2.To clear lowest set bit: `n &= (n - 1)` in a loop until `n == 0`.
3.To check if k-th bit is set: `(n & (1 << k)) != 0`.
4.To toggle k-th bit: `n ^= (1 << k)`.
5.To check power of two: `n > 0 and (n & (n - 1)) == 0`.
Watch for (Interview Traps)
Operator precedence: `==` has higher precedence than `&` in Python! Always wrap `(n & (1 << i)) != 0` in parentheses
Negative numbers in Python: Python uses arbitrary-precision integers, so bit shifts on negative numbers require masking with `0xFFFFFFFF`
Off-by-one in bit shifts: 0-indexed vs 1-indexed bit positions
Brian Kernighan's Bit Counting & XOR Reduction
def single_number(nums: list[int]) -> int:
# XOR Property: x ^ x = 0 and x ^ 0 = x
# All duplicate pairs cancel out, leaving the unique single number
unique = 0
for x in nums:
unique ^= x
return unique
def count_set_bits(n: int) -> int:
# Brian Kernighan's Algorithm: n & (n - 1) clears the lowest set bit
count = 0
while n > 0:
n &= (n - 1)
count += 1
return count
Cost
O(1) or O(number of set bits) · O(1) strictly constant memory (Hardware-level instructions with blazing fast execution.)
Canonical problems
#136 Single Number:XOR all numbers to cancel duplicates in O(N) time and O(1) space
#191 Number of 1 Bits:Clear lowest bit with n & (n - 1) in loop
#231 Power of Two:n > 0 and (n & (n - 1)) == 0
#338 Counting Bits:DP on bits: dp[i] = dp[i >> 1] + (i & 1)
What is the Bit Manipulation pattern in coding interviews?
O(1) low-level bitwise operations, masks, and XOR parity tricks It is categorized under Advanced & DP and features 238 curated interview problems across Easy (45), Medium (110), and Hard (83) difficulties.
Which tech companies frequently test Bit Manipulation?
Companies frequently asking Bit Manipulation questions include Google, Amazon, Microsoft, Meta, Bloomberg, NeetCode 150.
How do I master the Bit Manipulation pattern?
Start by studying the algorithmic strategy and template code, then practice Easy and Medium problems before attempting Hard problems under timed conditions.