Elias-Golomb Code Calculator

Variable-length integer codes, both directions.

  • Encode and decode
  • Best parameter
  • Bit counts
A small rounded clay stool with four clay rods of different lengths lying on its top, beside a clay bowl full of round beads, with two loose beads in front.

The guide

Short codes for small numbers: how gamma, Exp-Golomb and Golomb codes spend bits

Most data holds many small numbers and a few big ones. A unary prefix that announces the size lets the small ones travel in a handful of bits.

  • 37 bits Exp-Golomb code of 0, 1, 2, 3, 7, 8 and 42 (fixed width: 42)
  • 34 bits Golomb code of the same list with M = 10
  • M = 3 best divisor when p = 0.2 (3.6393 bits against 3.6096)
  • 0001001 Exp-Golomb code of 8, which is x + 1 = 1001 with three zeros
Read the full guide Fold the guide 8 min · 15 sources

Store the numbers 0, 1, 2, 3, 7, 8 and 42 the plain way and you need six bits for each, because 42 needs six. That is 42 bits for seven numbers, and six of the seven are wasting bits. Fixed width charges every number for the size of the largest one. A variable-length code does the opposite: it lets a small number take a short codeword and makes only the large ones pay.

The bargain only works if a reader can tell where one codeword ends. A prefix code guarantees it: no whole codeword is the beginning of another, so the stream needs no marker between words. Every code in this essay has that property.

A prefix that announces the size

The simplest prefix code for whole numbers is unary: the number n is written as n ones and then a zero. The cost grows in step with the value, which is wasteful for anything large, but the zero at the end is a perfect stop sign. A reader counts the ones, meets the zero and knows the number is finished.

Elias gamma coding turns the idea around to save space. It does not count the whole number in unary. It counts only how many bits the number has. For a number x of 1 or more, write N zeros, where N is the highest power of two that fits in x, and then write x itself in binary, which takes N + 1 bits. The zeros announce the size, the leading 1 ends the announcement, and the remaining N bits carry the detail. Read another way, N is sent in unary, as N zeros and a one, and the last N bits of x follow it.

The result is a code whose length is 2 times N plus 1. The number 1 costs a single bit, written 1. The numbers 2 and 3 cost three bits, 010 and 011. The numbers 4 to 7 cost five, and 8 to 15 cost seven. Doubling the number adds only two bits, which is far gentler than unary.

Gamma has a price at both ends, and the source page for the code shows both. Storing 5 in a fixed eight bits gives 00000101, while gamma writes 00101, three bits fewer. Storing 254 in eight bits is cheaper than its gamma code, which runs to fifteen, seven bits more. A code that favours small values has to be paid for by large ones.

Gamma starts at 1, so it cannot write zero. Add 1 before coding and subtract 1 after decoding, and that is the switch on this page that counts from zero.

Exp-Golomb: gamma that starts at zero

The Exp-Golomb code is that remedy made official. To code a non-negative integer x, write x + 1 in binary, count the bits, subtract one, and put that many zeros in front. The number 8 becomes 9, which is 1001; four bits minus one is three zeros, so the code is 0001001. Leave out the plus one and you would get 0001000, which is the code of 7. The code of x is identical to the gamma code of x + 1.

This is not a curiosity. Exp-Golomb codes are used in the H.264 and H.265 video compression standards, together with a variation that maps signed numbers onto the non-negative ones in the order 0, 1, minus 1, 2, minus 2. The page offers the same kind of folding, a zig-zag that sends 0, minus 1, 1, minus 2, 2 to 0, 1, 2, 3, 4.

A parameter k generalises the code. To write x at order k, code floor of x divided by 2 to the k in the order-0 form, then append the k low bits of x as they are. Order 2 writes 3 as 111, with no zeros in front. Raising k spends bits on small numbers and saves them on large ones, so the right order depends on how large the typical number is.

Here is the worked example of the whole list. The numbers 0, 1, 2, 3, 7, 8 and 42 code in order 0 as 1, 010, 011, 00100, 0001000, 0001001 and 00000101011. The lengths are 1, 3, 3, 5, 7, 7 and 11, a total of 37 bits against 42 for fixed width. Most of the saving comes from the four smallest numbers; 42 alone costs more than it would in six fixed bits.

Golomb and Rice: splitting by a divisor

Solomon Golomb described his family of codes in the 1960s. Instead of counting bits, a Golomb code divides. Pick a divisor M. For a number x, the quotient q is x divided by M, rounded down, and the remainder r is what is left. The quotient goes out in unary as q ones and a stop zero, and the remainder follows in truncated binary.

Truncated binary is the clever part. A remainder can be any value from 0 to M minus 1, and when M is a power of two that takes exactly log2 M bits. When M is not a power of two, plain binary would waste codewords. Take five symbols: three bits would hold eight, so three codewords go unused. Truncated binary gives the first three symbols two bits each, 00, 01 and 10, and the last two three bits, 110 and 111.

In Golomb terms, let b be the floor of log2 M and let the cutoff be 2 to the b plus 1, minus M. A remainder below the cutoff takes b bits. A remainder at or above the cutoff takes b + 1 bits, and what is written is the remainder plus the cutoff. The decoder reads b bits, and a value below the cutoff is the remainder; otherwise it reads one more bit and subtracts the cutoff. For M = 10 the value of b is 3 and the cutoff is 16 minus 10, which is 6. The remainder 5 is 101, and the remainder 6 becomes 12 in four bits, 1100.

Take 42 with M = 10. The quotient is 4 and the remainder is 2, so the code is 11110 followed by 010, which is 11110010. The same seven numbers cost 34 bits with this divisor. The numbers 0, 1, 2 and 3 take four bits each, 7 and 8 take five, and 42 takes eight. A coder that spent b + 1 bits on every remainder would need 39, five more, which is the saving that truncation earns.

Rice coding is the special case where M is a power of two, named for Robert Rice. With M = 8, the number 37 has quotient 4 and remainder 5, and the code is 11110101. The restriction turns division and multiplication by M into shifts, which is convenient on a computer, even though a Golomb code with a nearby divisor may be a little shorter.

Choosing the divisor from the source

Golomb himself was coding run lengths, and the natural model is a geometric one: the chance of a value x is p times (1 minus p) to the x, where p is the chance of a zero. Under that law a Golomb code is an optimal prefix code, provided the divisor is right, and the right divisor is the ceiling of minus the natural log of 2 minus p, divided by the natural log of 1 minus p.

For p = 0.2 the formula gives minus ln 1.8 over ln 0.8, which is 2.634, so M = 3. A value of p of 0.5 gives M = 1, plain unary. The divisor grows as zeros become rarer: p = 0.1 gives 7 and p = 0.05 gives 14. A source in which zero is common wants a small divisor, because most values are near the front. One in which zero is rare spreads its values out and wants a wide bin.

How good is the choice? The entropy of the geometric source is minus p log2 p minus (1 minus p) log2 (1 minus p), all divided by p. At p = 0.2 that is 3.6096 bits. The Golomb code with M = 3 averages 3.6393 bits, a gap of about 0.03 bits. The page computes the average two ways, by a closed form and by adding up thousands of terms of the law, and checks the formula against a brute-force search over the divisors. It reads the gap for any p between 0.001 and 0.999 and keeps a table of seven fixed values.

Where the codes are used, and where they stop

The common thread is prediction. A predictor guesses the next sample and the coder stores the error, and errors cluster near zero. The specification of the FLAC audio format defines its Rice code as a variable-length code that uses a short code for samples close to zero and a progressively longer code for samples further away, exploiting the fact that residuals are often small.

Gamma and its relatives suit a different job: coding numbers whose upper bound is not known in advance. A universal code is a prefix code that stays within a constant factor of the best possible code for any distribution whose probabilities never rise as the numbers grow. It gives up the last drop of efficiency in exchange for needing no knowledge of the source.

The page checks every code set for the prefix property by sorting the codewords and comparing neighbours, since a codeword that begins another must land right beside it. It also adds the Kraft sum, the total of 2 to the power of minus each length. A prefix code can never exceed 1, and a total below 1 means spare codewords. For gamma over the numbers 1 to 4095 the sum is exactly 1 minus 2 to the power of minus 12, worked out with whole numbers.

Two limits matter. The divisor is only optimal when the numbers really are geometric. A source with a hump in the middle, such as a sine wave, does not suit these codes, and the Golomb page itself warns of this. And exact arithmetic has a ceiling: a JavaScript number holds integers exactly only up to 2 to the 53 minus 1, so values above that are refused rather than rounded.

The independent check ran in Python with exact fractions and decimals, using a separate program written from the same descriptions. It agreed with the page on 2,815 codewords from a fixed list, with their decoding, and on the best divisor for every p from 0.001 to 0.999. The heavy sums run in a worker thread, which keeps the page responsive. Huffman and arithmetic coding, which fit the code to the data instead of assuming a shape, are separate topics and are not covered here.

Sources

Every source consulted for this guide. Links checked November 11, 2026.

  1. Elias gamma coding Wikipedia
  2. Golomb coding Wikipedia
  3. Exponential-Golomb coding Wikipedia
  4. Unary coding Wikipedia
  5. Truncated binary encoding Wikipedia
  6. Prefix code Wikipedia
  7. Kraft-McMillan inequality Wikipedia
  8. Universal code (data compression) Wikipedia
  9. Geometric distribution Wikipedia
  10. Entropy (information theory) Wikipedia
  11. RFC 9639: Free Lossless Audio Codec (FLAC) IETF
  12. BigInt - JavaScript MDN Web Docs
  13. Web Workers API - Web APIs MDN Web Docs
  14. fractions (Python documentation) Python Software Foundation
  15. decimal (Python documentation) Python Software Foundation