Bit operations / bit manipulations [mp]
Micro pattern¶
Description¶
Use masks, shifts, and bitwise identities to represent states or perform arithmetic compactly.
Problems¶
XOR cancellation and comparison¶
XOR removes equal pairs or identifies positions where two values differ.
- 136. Single Number (Top Easy)
- 461. Hamming Distance (Top Easy)
Remove the lowest set bit¶
The expression \(n \mathbin{\&} (n-1)\) clears the least-significant set bit.
- 191. Number of Bits (Top Easy)
- 461. Hamming Distance (Top Easy)
Shift and append bits¶
Open a bit position and append a bit from the source.
- 190. Reverse Bits (Top Easy)
Bitwise arithmetic¶
Use shifts and masks for powers of two or fixed-width arithmetic.
- 29. Divide Two Integers (Top Medium)
- 50. Pow(x, n) (Top Medium)
- 371. Sum of Two Integers (Top Medium)
- 315. Count of Smaller Numbers After Self (Top Hard)
Packed state¶
Store multiple Boolean conditions in different bits of an integer.
- 289. Game of Life (Top Hard)
- 864. Shortest Path to Get All Keys (Graph Theory)