← All topics
Bit Manipulation
Masks, shifts, and XOR tricks.
Bit Manipulation
Masks, shifts, and XOR identities.
Core syntax
- Test bit i —
n & (1 << i). - Lowest set bit —
n & (-n); clear it —n & (n - 1). - Count bits —
n.bit_count()(3.10+) orbin(n).count('1').
missing = 0
for i, x in enumerate(nums):
missing ^= i ^ x
missing ^= len(nums) # XOR cancels pairs
Watch out
x ^ x == 0andx ^ 0 == x— the whole trick behind XOR puzzles.