Every time your computer stores the number 3, it wastes 29 bits: a 32-bit integer spends most of its capacity on leading zeros. That is fine when you know in advance that numbers stay below four billion. But what if you are streaming an endless sequence of integers and most of them are small?
Elias gamma coding, published by Peter Elias in 1975, solves this with a beautifully simple idea: write the binary representation of a number, then prefix it with a unary count of how many bits that representation has. The result is a self-delimiting bitstream — a decoder reading the stream knows exactly where each codeword ends without any length field or separator byte.
The code is universal: it is optimal (up to a constant factor) for any source whose probabilities decrease monotonically, without knowing the distribution in advance. Small integers get short codes; large ones get longer codes; but no integer is ever left out.
Elias gamma is the foundation under a family of related codes — Elias delta, Fibonacci coding, compression algorithms, and the inverted indexes behind every modern search engine.
Comments
Loading comments...