Elias Universal Integer Codes
Decode self-delimiting integer lengths and distinguish incomplete headers from valid messages.
By Riano · Published · Updated · Report a correction
Elias Universal Integer Codes
Elias universal codes (such as Gamma and Delta codes) are used to encode positive integers so that the resulting codewords are prefix-free. This self-delimiting property allows a receiver to decode a sequence of numbers without needing to know beforehand how many bits represent each number.
Worked Example: Delta Encoding
Let's see how Elias Delta coding encodes the integer 16. Delta coding encodes the length of the binary representation using Gamma coding, and then appends the binary representation without its leading 1.
- The binary representation of 16 is
10000. - The length of this binary string is 5.
- We Gamma-encode the length 5. The binary representation of 5 is
101, which has a length of 3. Thus, its Gamma code consists of 2 zeros followed by the binary string:00101. - The payload is the binary representation of 16 without its leading 1:
0000. - Appending the payload to the Gamma-encoded length yields the full codeword:
00101+0000=001010000.
Interactive Experiment
You can test different integer values and encoding types using the Elias Universal Integer Codes module.
- Select the Code as "Delta".
- Set the Integer to 16. Observe the generated "Full codeword"
001010000and how the bit trace separates the unary length from the binary size and payload. - Change the Truncate setting to "Drop final bit".
- Observe that the status changes to "Truncated code" and the "Decoded integer" becomes "Unavailable", demonstrating that prefix codes rely on the complete sequence to be decodable.
Limitations
Elias codes are defined strictly for positive integers (i.e., integers greater than 0). There is no native provision for zero or negative integers unless a mapping function (like zigzag encoding) is applied first. Additionally, while universal, they are not optimal for every probability distribution. For instance, if small numbers are extremely frequent, simpler codes like unary might be shorter, whereas if the numbers are uniformly distributed over a known finite range, fixed-length binary codes are more efficient.
References
For more detailed information regarding universal codeword lengths, refer to Peter Elias' original paper on the topic: Universal Codeword Lengths and Representations.
Experience it interactively
Adjust parameters, observe in real time, and build deep intuition with Riano’s interactive Elias Universal Integer Codes module.
Try Elias Universal Integer Codes on Riano →