Elias gamma encoding
Encode positive integers as Elias gamma codes, or decode a gamma bitstream back to numbers.
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.