Skip to content
uencoder

Unary Coding

Unary coding encodes a non-negative integer n as n ones followed by a zero (or the reverse); it is the simplest prefix code and optimal for very small values.

Purpose

It serves as the quotient component of Golomb/Rice coding, or encodes integers from extremely small-valued distributions on its own.

Mechanism

The integer n becomes 1^n0 — n consecutive ones followed by a zero. Decoding counts the leading ones. The code length is n+1, which is efficient only for geometric distributions with very small n.

Use cases

  • Quotient coding in Golomb/Rice
  • Encoding very small integers
  • Teaching examples

Related tools

References