Bit I/O & Universal Codes
- Compression is bit-precise, not byte-precise
- MSB-first bit order throughout this pack
- Universal codes show integer coding costs
1 / 7
Bit order (MSB-first)
- First bit -> highest bit of byte
- WriteBits uses lowest n bits of value
- Padding with zeros at end
2 / 7
Common bit I/O bugs
- Off-by-one on partial bytes
- Reversed bit order
- Missing or wrong padding
3 / 7
Elias gamma
- k zeros where k = floor(log2 n)
- Then binary of n (k+1 bits)
- Prefix-free
4 / 7
Elias delta
- Gamma-code the bit-length L
- Append lower L-1 bits of n
- Better for large n
5 / 7
Why universal codes
- No model required
- Length grows ~ log2 n
- Not optimal when model known
6 / 7
What you will build
- Bitstream I/O
- Elias gamma/delta
7 / 7
Use arrow keys or click edges to navigate. Press H to toggle help, F for fullscreen.