Elias delta coding
Elias delta code is a universal code encoding the positive integers developed by Peter Elias.[1]: 200
Encoding
To code a number X≥1:
- Let N=⌊log2 X⌋ be the highest power of 2 in X, so 2N ≤ X < 2N+1.
- Let L=⌊log2 N+1⌋ be the highest power of 2 in N+1, so 2L ≤ N+1 < 2L+1.
- Write L zeros, followed by
- the L+1-bit binary representation of N+1, followed by
- all but the leading bit (i.e. the last N bits) of X.
An equivalent way to express the same process:
- Separate X into the highest power of 2 it contains (2N) and the remaining N binary digits.
- Encode N+1 with Elias gamma coding.
- Append the remaining N binary digits to this representation of N+1.
To represent a number , Elias delta uses bits.[1]: 200
The code begins, using instead of :
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 | 00 1 01 0001 |
1/512 |
To decode an Elias delta-coded integer:
- Read and count zeros from the stream until you reach the first one. Call this count of zeros L.
- Considering the one that was reached to be the first digit of an integer, with a value of 2L, read the remaining L digits of the integer. Call this integer N+1, and subtract one to get N.
- Put a one in the first place of our final output, representing the value 2N.
- Read and append the following N digits.
resultaten Wetsartikelen: 72 Jurisprudentie: 0 Professionals: 0 Wetten BURGERLIJK WETBOEK BOEK 1 ART.:302 303 BURGERLIJK WETBOEK BOEK 2 ART.:303 BURGERLIJK WETBOEK BOEK 3 ART.:303 BURGERLIJK WETBOEK BOEK 7 ART.:303 304 305 BURGERLIJK WETBOEK BOEK 8 ART.:790 FAILLISSEMENTSWET ART.:303 358 359 GEMEENTEWET ART.:303 RIJKSWET HOUDENDE MACHTIGING TOT DEELNEMING DOOR HET... ART.:1 RIJKSWET HOUDENDE MACHTIGING TOT VERHOGING VAN DE ... ART.:1 RIJKSWET MACHTIGING TOT DEELNEMING DOOR HET KONINKRIJK... ART.:1 UITVOERINGSWET EUROPEES VERDRAG INZAKE RECHTSPOSITIE... ART.:2 VERZAMELWET SOCIALE VERZEKERINGEN 2007 ART.:XIII WEGENVERKEERSWET 1994 ART.:179a WET ADMINISTRATIEFRECHTELIJKE HANDHAVING VERKEERSVOORSCHRIFT... ART.:44 WETBOEK VAN MILITAIR STRAFRECHT ART.:141 WETBOEK VAN STRAFRECHT ART.:303 304 304a 304b 305 43b 5 WETBOEK VAN STRAFVORDERING ART.:303 WET DUALISERING GEMEENTEBESTUUR ART.:VI WET OP DE ARBEIDSONGESCHIKTHEIDSVERZEKERING ART.:10 WET OP DE OMZETBELASTING 1968 ART.:55 WET OP HET HOGER ONDERWIJS EN WETENSCHAPPELIJK ONDERZOEK ART.:18.28 18.29 18.30 WET OVERDRACHT TAKEN OGB ART.:III WET PRIVATISERING ABP ART.:8 WET PRIVATISERING SPOORWEGPENSIOENFONDS ART.:6 WET RUIMTELIJKE ORDENING ART.:8.3 WET TARIEVEN IN BURGERLIJKE ZAKEN ART.:2 WET VERHOGING UITKERINGSHOOGTE ARBEIDSONGESCHIKTHEIDSWETTEN ART.:V WET WERK EN INKOMEN NAAR ARBEIDSVERMOGEN ART.:123 ZIEKTEWET ART.:11
0 - 5 van de 72 zoekresultaten algemene Wet Bijzondere Ziektekosten | artikel 16a wet / artikel
Burgerlijk Wetboek Boek 8 | artikel 790 wet / artikel
Wetboek van Burgerlijke Rechtsvordering | artikel 790 wet / artikel
[Vervallen per 01-01-2002] Wetboek van Koophandel | artikel 790 wet / artikel
[Vervallen per 01-04-1991] Burgerlijk Wetboek Boek 8 | artikel 790 wet / artikel
Een in de openbare registers teboekstaand binnenschip is een registergoed.
meer resultaten
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.
Example code
Encoding
void eliasDeltaEncode(char* source, char* dest)
{
IntReader intreader(source);
BitWriter bitwriter(dest);
while (intreader.hasLeft())
{
int num = intreader.getInt();
int len = 0;
int lengthOfLen = 0;
for (int temp = num; temp > 0; temp >>= 1) // calculate 1+floor(log2(num))
len++;
for (int temp = len; temp > 1; temp >>= 1) // calculate floor(log2(len))
lengthOfLen++;
for (int i = lengthOfLen; i > 0; --i)
bitwriter.outputBit(0);
for (int i = lengthOfLen; i >= 0; --i)
bitwriter.outputBit((len >> i) & 1);
for (int i = len-2; i >= 0; i--)
bitwriter.outputBit((num >> i) & 1);
}
bitwriter.close();
intreader.close();
}
Decoding
void eliasDeltaDecode(char* source, char* dest)
{
BitReader bitreader(source);
IntWriter intwriter(dest);
while (bitreader.hasLeft())
{
int num = 1;
int len = 1;
int lengthOfLen = 0;
while (!bitreader.inputBit()) // potentially dangerous with malformed files.
lengthOfLen++;
for (int i = 0; i < lengthOfLen; i++)
{
len <<= 1;
if (bitreader.inputBit())
len |= 1;
}
for (int i = 0; i < len-1; i++)
{
num <<= 1;
if (bitreader.inputBit())
num |= 1;
}
intwriter.putInt(num); // write out the value
}
bitreader.close();
intwriter.close();
}
Generalizations
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).
References
- ^ a b Elias, Peter (March 1975). "Universal codeword sets and representations of the integers". IEEE Transactions on Information Theory. 21 (2): 194–203. doi:10.1109/tit.1975.1055349.