Elias δ code or Elias delta code is a universal code encoding the positive integers developed by Peter Elias.
To code a number X ≥ 1:
An equivalent way to express the same process:
To represent a number
x
\lfloorlog2(x)\rfloor+2\lfloorlog2(\lfloorlog2(x)\rfloor+1)\rfloor+1
log2(\lfloorlog2(x)\rfloor+1)
The code begins, using
\gamma'
\gamma
Number | N | N+1 | δ encoding | Implied probability | |
---|---|---|---|---|---|
1 = 20 | 0 | 1 | 1 | 1/2 | |
2 = 21 + 0 | 1 | 2 | 0 1 0 ''0'' | 1/16 | |
3 = 21 + 1 | 1 | 2 | 0 1 0 ''1'' | 1/16 | |
4 = 22 + 0 | 2 | 3 | 0 1 1 ''00'' | 1/32 | |
5 = 22 + 1 | 2 | 3 | 0 1 1 ''01'' | 1/32 | |
6 = 22 + 2 | 2 | 3 | 0 1 1 ''10'' | 1/32 | |
7 = 22 + 3 | 2 | 3 | 0 1 1 ''11'' | 1/32 | |
8 = 23 + 0 | 3 | 4 | 00 1 00 ''000'' | 1/256 | |
9 = 23 + 1 | 3 | 4 | 00 1 00 ''001'' | 1/256 | |
10 = 23 + 2 | 3 | 4 | 00 1 00 ''010'' | 1/256 | |
11 = 23 + 3 | 3 | 4 | 00 1 00 ''011'' | 1/256 | |
12 = 23 + 4 | 3 | 4 | 00 1 00 ''100'' | 1/256 | |
13 = 23 + 5 | 3 | 4 | 00 1 00 ''101'' | 1/256 | |
14 = 23 + 6 | 3 | 4 | 00 1 00 ''110'' | 1/256 | |
15 = 23 + 7 | 3 | 4 | 00 1 00 ''111'' | 1/256 | |
16 = 24 + 0 | 4 | 5 | 00 1 01 ''0000'' | 1/512 | |
17 = 24 + 1 | 4 | 5 | 1/512 |
To decode an Elias delta-coded integer:
Example: 001010011 1. 2 leading zeros in 001 2. read 2 more bits i.e. 00101 3. decode N+1 = 00101 = 5 4. get N = 5 − 1 = 4 remaining bits for the complete code i.e. '0011' 5. encoded number = 24 + 3 = 19
This code can be generalized to zero or negative integers in the same ways described in Elias gamma coding.
Elias delta coding does not code zero or negative integers.One way to code all non negative integers is to add 1 before coding and then subtract 1 after decoding.One way to code all integers is to set up a bijection, mapping integers all integers (0, 1, −1, 2, −2, 3, −3, ...) to strictly positive integers (1, 2, 3, 4, 5, 6, 7, ...) before coding.This bijection can be performed using the "ZigZag" encoding from Protocol Buffers (not to be confused with Zigzag code, nor the JPEG Zig-zag entropy coding).