Unary coding, 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:

{| class="wikitable"

! 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 encoding for the following discrete probability distribution

:<math>\operatorname{P}(n) = 2^{-n}\,</math>

for <math>n=1,2,3,...</math>.

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

:<math>\operatorname{P}(n) = (k-1)k^{-n}\,</math>

for which k &ge; &phi; = 1.61803398879..., the golden ratio, or, more generally, for any discrete distribution for which

:<math>\operatorname{P}(n) \ge \operatorname{P}(n+1) + \operatorname{P}(n+2)\, </math>

for <math>n=1,2,3,...</math>. 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.

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.

References