4. Bit Manipulation
AND, OR, XOR, shifts and the classic bit tricks.
Your progress0 of 12
8easy
4medium
0hard
Understand the topic
Every int is 32 switches. Bit manipulation means flipping and reading those switches directly with & (both on), | (either on), ^ (different), ~ (flip) and shifts (<< and >>).
Three tricks solve most problems here: x & (x - 1) removes the lowest 1-bit, x & -x keeps only the lowest 1-bit, and a ^ a = 0 makes pairs cancel out. In Java, use >>> to shift right without copying the sign bit.
| # | Problem | Pattern | Level | ||
|---|---|---|---|---|---|
| 1 | Number of 1 Bits #191 | n & (n-1) | Easy | LeetCode ↗ | |
| 2 | Power of Two #231 | n & (n-1) | Easy | LeetCode ↗ | |
| 3 | Single Number #136 | XOR | Easy | LeetCode ↗ | |
| 4 | Counting Bits #338 | Bits + DP | Easy | LeetCode ↗ | |
| 5 | Missing Number #268 | XOR | Easy | LeetCode ↗ | |
| 6 | Reverse Bits #190 | Shifts | Easy | LeetCode ↗ | |
| 7 | Hamming Distance #461 | XOR + popcount | Easy | LeetCode ↗ | |
| 8 | Complement of Base 10 Integer #1009 | Bit masks | Easy | LeetCode ↗ | |
| 9 | Single Number II #137 | Bit counting | Medium | LeetCode ↗ | |
| 10 | Single Number III #260 | XOR + lowest set bit | Medium | LeetCode ↗ | |
| 11 | Sum of Two Integers #371 | XOR + carry | Medium | LeetCode ↗ | |
| 12 | Bitwise AND of Numbers Range #201 | Common prefix | Medium | LeetCode ↗ |
Nothing matches that filter.
Work top to bottom — each list goes from easy to hard. Try a problem for at least 15 minutes before opening a hint. Ticks are saved in your browser.
