Bit Manipulation
Treat a number as a row of on/off switches: a handful of one-line tricks replace loops, store sets in a single integer, and enumerate every subset.
After this lesson you will be able to
- Read and write numbers in binary, and use
&,|,^,~,<<and>>correctly (including operator precedence). - Test, set, clear and toggle a single bit, and isolate or erase the lowest set bit.
- Count set bits, test powers of two, and use XOR's cancelling property to find a lonely or missing number.
- Use a bitmask to represent a subset, and enumerate all subsets and submasks.
- Know how Python's unlimited and negative integers behave, and when a problem is a bit-manipulation problem.