Skip to content
100% local

Elias gamma encoding

Encode positive integers as Elias gamma codes, or decode a gamma bitstream back to numbers.

Input
Output

Elias gamma encoding

Elias gamma coding is a universal code for positive integers: each number becomes a run of zero bits giving its magnitude, followed by its binary digits. Small numbers take very few bits — 1 is just "1", 5 is "00101" — which is why the scheme shows up in real compressors, from search-index posting lists to LZ77-style length and distance pairs. Paste a list of numbers and this tool builds the exact bitstream, or paste a bitstream and it reads the numbers back out.

Direction switches between encoding and decoding. Encoding reads decimal numbers, hexadecimal numbers, or plain text — in text mode every character is encoded by its Unicode code point, offset by 1 so a zero code point can still be represented, an offset removed automatically on decode. Decimal and hexadecimal modes use the numbers exactly as typed, matching a textbook gamma table. Decoding reads a bitstream of 0s and 1s, ignoring spaces and line breaks, and writes the recovered numbers back in whichever of those three formats you choose.

Two options handle real-world round-tripping. "Pad encoded bits to a whole byte" appends trailing zero bits so the result lines up on a byte boundary, and tells the decoder to treat a short trailing run of zeros as padding rather than truncated data. "Wrap output" breaks a long bitstream into fixed-width lines; it is skipped when decoding to text, since a line break would change the recovered text. Entering 0 in decimal or hex mode is flagged with an exact message, since gamma codes only represent 1 and above.

Everything runs locally in your browser — nothing you type is uploaded or stored. Copy the result, download it as a .txt file, or send the output back into the input to decode what you just encoded.

FAQ

What is Elias gamma coding used for?
It is a universal code for positive integers used inside real compression formats — for example the gap lengths between entries in a search-index posting list, or the length/distance values inside LZ77-style compressors — because small numbers need only a handful of bits.
Why do I get an error when I enter 0?
Elias gamma coding can only represent integers of 1 or greater in decimal or hexadecimal mode. Switch the input format to text if you need to encode a zero value — it is shifted by 1 internally and restored automatically on decode.
What does "pad encoded bits to a whole byte" do?
On encode it adds trailing zero bits so the result is a whole number of bytes. On decode it tells the tool that a short trailing run of zero bits is that padding rather than a sign of missing data.
Can I decode a bitstream I generated somewhere else?
Yes, as long as it only contains 0s, 1s and whitespace. Match the padding setting to how it was produced, and pick the output format you want the recovered numbers in.
Is my data uploaded anywhere?
No. Encoding and decoding run entirely in your browser — nothing you paste ever leaves your device.