Bit Manipulation
Two halves. Learn builds the model and the vocabulary. Practice is where you use it on real problems.
Learn
- Bit Basics - the survival page: why binary at all, place values, the four operators, shifts, two's complement, the Python gotchas. After this: you can read and write any single bitwise expression with confidence.
- Core Techniques - set, clear, toggle, rotate, the two identities that carry most problems, reversing bits, the XOR swap, bitmask sets, and the full idiom cheatsheet. This is the page to actually memorize - it covers what most interview problems need.
Practice
Five pages, ordered by what you do to the bits: count them, read their shape, build a new number out of them, cancel them, or treat them as a set.
- Counting Bits - popcount, Hamming distance, and using the set-bit count as a key or filter.
- Bit Patterns & Properties - powers of two, runs and gaps, and folding an operator across a whole range.
- Rebuilding a Number - complement, reverse, encode/decode, and arithmetic without
+. - XOR - cancellation, single number, and folding or inverting a sequence.
- Bitmask as a Set - one integer models a set of small values, every operation O(1).