# Unary coding

Unary coding, or the unary numeral system and also sometimes called thermometer code, is an entropy encoding that represents a natural number, n, with a code of length n + 1 ( or n ), usually n ones followed by a zero (if natural number is understood as non-negative integer) or with n − 1 ones followed by a zero (if natural number is understood as strictly positive integer). For example 5 is represented as 111110 or 11110. Some representations use n or n − 1 zeros followed by a one. The ones and zeros are interchangeable without loss of generality. Unary coding is both a prefix-free code and a self-synchronizing code.
| n (non-negative) | n (strictly positive) | Unary code | Alternative |
| 0 | 1 | 0 | 1 |
| 1 | 2 | 10 | 01 |
| 2 | 3 | 110 | 001 |
| 3 | 4 | 1110 | 0001 |
| 4 | 5 | 11110 | 00001 |
| 5 | 6 | 111110 | 000001 |
| 6 | 7 | 1111110 | 0000001 |
| 7 | 8 | 11111110 | 00000001 |
| 8 | 9 | 111111110 | 000000001 |
| 9 | 10 | 1111111110 | 0000000001 |
Unary coding is an optimally efficient encoding for the following discrete probability distribution
    $\operatorname {P} (n)=2^{-n}\,$
for $n=1,2,3,...$.
In symbol-by-symbol coding, it is optimal for any geometric distribution
    $\operatorname {P} (n)=(k-1)k^{-n}\,$
for which k ≥ φ = 1.61803398879..., the golden ratio, or, more generally, for any discrete distribution for which
    $\operatorname {P} (n)\geq \operatorname {P} (n+1)+\operatorname {P} (n+2)\,$
for $n=1,2,3,...$. 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 code in use today

Examples of unary code uses include:
* In Golomb Rice code, unary encoding is used to encode the quotient part of the Golomb code word.
* In UTF-8, unary encoding is used in the leading byte of a multi-byte sequence to indicate the number of bytes in the sequence so that the length of the sequence can be determined without examining the continuation bytes.
* Instantaneously trained neural networks use unary coding for efficient data representation.

## Unary coding in biological networks

Unary coding is used in the neural circuits responsible for birdsong production. The nucleus in the brain of the songbirds that plays a part in both the learning and the production of bird song is the HVC (high vocal center). The command signals for different notes in the birdsong emanate from different points in the HVC. This coding works as space coding which is an efficient strategy for biological circuits due to its inherent simplicity and robustness.

## Standard run-length unary codes

All binary data is defined by the ability to represent unary numbers in alternating run-lengths of 1s and 0s. This conforms to the standard definition of unary i.e. N digits of the same number 1 or 0. All run-lengths by definition have at least one digit and thus represent strictly positive integers.
| n | RL code | Next code |
| 1 | 1 | 0 |
| 2 | 11 | 00 |
| 3 | 111 | 000 |
| 4 | 1111 | 0000 |
| 5 | 11111 | 00000 |
| 6 | 111111 | 000000 |
| 7 | 1111111 | 0000000 |
| 8 | 11111111 | 00000000 |
| 9 | 111111111 | 000000000 |
| 10 | 1111111111 | 0000000000 |
These codes are guaranteed to end validly on any length of data ( when reading arbitrary data ) and in the ( separate ) write cycle allow for the use and transmission of an extra bit ( the one used for the first bit ) while maintaining overall and per-integer unary code lengths of exactly N.

## Uniquely decodable non-prefix unary codes

Following is an example of uniquely decodable unary codes that is not a prefix code and is not instantaneously decodable (need look-ahead to decode)
| n | Unary code | Alternative |
| 1 | 1 | 0 |
| 2 | 10 | 01 |
| 3 | 100 | 011 |
| 4 | 1000 | 0111 |
| 5 | 10000 | 01111 |
| 6 | 100000 | 011111 |
| 7 | 1000000 | 0111111 |
| 8 | 10000000 | 01111111 |
| 9 | 100000000 | 011111111 |
| 10 | 1000000000 | 0111111111 |
These codes also ( when writing unsigned integers ) allow for the use and transmission of an extra bit ( the one used for the first bit ). Thus they are able to transmit 'm' integers * N unary bits and 1 additional bit of information within m*N bits of data.

## Symmetric unary codes

Following set of unary codes are symmetric and can be read in any direction. It is also instantaneously decodable in either direction.
| n (strictly positive) | Unary code | Alternative | n (non-negative) |
| 1 | 1 | 0 | 0 |
| 2 | 00 | 11 | 1 |
| 3 | 010 | 101 | 2 |
| 4 | 0110 | 1001 | 3 |
| 5 | 01110 | 10001 | 4 |
| 6 | 011110 | 100001 | 5 |
| 7 | 0111110 | 1000001 | 6 |
| 8 | 01111110 | 10000001 | 7 |
| 9 | 011111110 | 100000001 | 8 |
| 10 | 0111111110 | 1000000001 | 9 |

## Canonical unary codes

For unary values where the maximum is known, one can use canonical unary codes that are of a somewhat numerical nature and different from character based codes. It involves starting with numerical '0' or '-1' ( $\operatorname {2} ^{n}-1\,$) and the maximum number of digits then for each step reducing the number of digits by one and increasing/decreasing the result by numerical '1'.
| n | Unary code | Alternative |
| 1 | 1 | 0 |
| 2 | 01 | 10 |
| 3 | 001 | 110 |
| 4 | 0001 | 1110 |
| 5 | 00001 | 11110 |
| 6 | 000001 | 111110 |
| 7 | 0000001 | 1111110 |
| 8 | 00000001 | 11111110 |
| 9 | 000000001 | 111111110 |
| 10 | 000000000 | 111111111 |
Canonical codes can require less processing time to decode when they are processed as numbers not a string. If the number of codes required per symbol length is different to 1, i.e. there are more non-unary codes of some length required, those would be achieved by increasing/decreasing the values numerically without reducing the length in that case.

## Generalized unary coding

A generalized version of unary coding was presented by Subhash Kak to represent numbers much more efficiently than standard unary coding. Here's an example of generalized unary coding for integers from 0 through 15 that requires only 7 bits (where three bits are arbitrarily chosen in place of a single one in standard unary to show the number). Note that the representation is cyclic where one uses markers to represent higher integers in higher cycles.
| n | Unary code | Generalized unary |
| 0 | 0 | 0000000 |
| 1 | 10 | 0000111 |
| 2 | 110 | 0001110 |
| 3 | 1110 | 0011100 |
| 4 | 11110 | 0111000 |
| 5 | 111110 | 1110000 |
| 6 | 1111110 | 0010111 |
| 7 | 11111110 | 0101110 |
| 8 | 111111110 | 1011100 |
| 9 | 1111111110 | 0111001 |
| 10 | 11111111110 | 1110010 |
| 11 | 111111111110 | 0100111 |
| 12 | 1111111111110 | 1001110 |
| 13 | 11111111111110 | 0011101 |
| 14 | 111111111111110 | 0111010 |
| 15 | 1111111111111110 | 1110100 |
Generalized unary coding requires that the range of numbers to be represented to be pre-specified because this range determines the number of bits that are needed.