Elias δ code or Elias delta code is a universal code encoding the positive integers developed by Peter Elias.: 200
To code a number X ≥ 1:
An equivalent way to express the same process:
To represent a number , Elias delta (δ) uses bits.: 200 This is useful for very large integers, where the overall encoded representation's bits end up being fewer than what one might obtain using Elias gamma coding due to the portion of the previous expression.
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 | 0100 | 1/16 |
| 3 = 21 + 1 | 1 | 2 | 0101 | 1/16 |
| 4 = 22 + 0 | 2 | 3 | 01100 | 1/32 |
| 5 = 22 + 1 | 2 | 3 | 01101 | 1/32 |
| 6 = 22 + 2 | 2 | 3 | 01110 | 1/32 |
| 7 = 22 + 3 | 2 | 3 | 01111 | 1/32 |
| 8 = 23 + 0 | 3 | 4 | 00100000 | 1/256 |
| 9 = 23 + 1 | 3 | 4 | 00100001 | 1/256 |
| 10 = 23 + 2 | 3 | 4 | 00100010 | 1/256 |
| 11 = 23 + 3 | 3 | 4 | 00100011 | 1/256 |
| 12 = 23 + 4 | 3 | 4 | 00100100 | 1/256 |
| 13 = 23 + 5 | 3 | 4 | 00100101 | 1/256 |
| 14 = 23 + 6 | 3 | 4 | 00100110 | 1/256 |
| 15 = 23 + 7 | 3 | 4 | 00100111 | 1/256 |
| 16 = 24 + 0 | 4 | 5 | 001010000 | 1/512 |
| 17 = 24 + 1 | 4 | 5 | 001010001 | 1/512 |
To decode an Elias delta-coded integer:
Example: 001010011
This code can be generalized to zero or negative integers in the same ways described in Elias gamma coding.
void eliasDeltaEncode(const ByteBuffer& source, ByteBuffer& dest) {
IntReader intreader(source);
BitWriter bitwriter(dest);
while (intreader.hasLeft()) {
int num = intreader.getInt();
int len = 1 + std::floor(std::log2(num));
int lengthOfLen = std::floor(std::log2(len));
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();
}
void eliasDeltaDecode(const ByteBuffer& source, ByteBuffer& dest) {
BitReader bitreader(source);
IntWriter intwriter(dest);
while (bitreader.hasLeft()) {
int num = 1;
int len = 1;
int lengthOfLen = 0;
// potentially dangerous with malformed files.
while (!bitreader.inputBit()) {
++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();
}
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 zigzag entropy coding).
The code lengths for Elias delta coding are: