Handbooks / Data Structures & Algorithms / Chapter 9
Bit Manipulation
7 pages · ~5 min✓ Reviewed
Builds on Dynamic Programming and Greedy. Next up: Math and Number Theory.
Bits and Binary Representation
1. Place Values
- Bit i has weight 2^i, counted from 0 at the right (the LSB)
- n bits hold 0 to 2^n - 1 unsigned
- An integer is a row of on/off switches
- Bit tricks flip the switches directly, not via arithmetic
13 to binary: divide by 2, read remainders bottom-up
- 113 / 2 = 6r 1
- 26 / 2 = 3r 0
- 33 / 2 = 1r 1
- 41 / 2 = 0r 1
- 513 = 11018+4+0+1