Unary coding is an optimally efficient encoding for the following discrete probability distribution 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.
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.
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. 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.
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 1 through 15 that requires only 7 bits. 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.