Unary coding

Unary coding,[nb 1] or the unary numeral system, is an entropy encoding that represents a natural number, n, with n ones followed by a zero (if the term natural number is understood as non-negative integer) or with n − 1 ones followed by a zero (if the term natural number is understood as strictly positive integer). A unary number's code length would thus be n + 1 with that first definition, or n with that second definition. Unary code when vertical behaves like mercury in a thermometer that gets taller or shorter as n gets bigger or smaller, and so is sometimes called thermometer code.[1] An alternative representation uses n or n − 1 zeros followed by a one, effectively swapping the ones and zeros, without loss of generality. For example, the first ten unary codes are:

Unary code Alternative n (non-negative) n (strictly positive)
0 1 0 1
10 01 1 2
110 001 2 3
1110 0001 3 4
11110 00001 4 5
111110 000001 5 6
1111110 0000001 6 7
11111110 00000001 7 8
111111110 000000001 8 9
1111111110 0000000001 9 10

Unary coding is an optimally efficient[clarification needed] encoding for the following discrete probability distribution[citation needed]

for .

In symbol-by-symbol coding, it is optimal for any geometric distribution

for which k ≥ φ = 1.61803398879..., the golden ratio, or, more generally, for any discrete distribution for which

for . Although it is the optimal symbol-by-symbol coding for such probability distributions, Golomb coding achieves better compression capability for the geometric distribution because it does not consider input symbols independently, but rather implicitly groups the inputs. For the same reason, arithmetic encoding performs better for general probability distributions, as in the last case above.

Unary coding is both a prefix-free code and a self-synchronizing code.


Cite error: There are <ref group=nb> tags on this page, but the references will not show without a {{reflist|group=nb}} template (see the help page).

  1. ^ "University of Alberta Dictionary of Cognitive Science: Thermometer Code". www.bcp.psych.ualberta.ca. Retrieved 2025-05-31.

© MMXXIII Rich X Search. We shall prevail. All rights reserved. Rich X Search