Unary coding,
or the
unary numeral system
The unary numeral system is the simplest numeral system to represent natural numbers: to represent a number ''N'', a symbol representing 1 is repeated ''N'' times.
In the unary system, the number 0 (zero) is represented by the empty string, tha ...
, is an
entropy encoding
In information theory, an entropy coding (or entropy encoding) is any lossless data compression method that attempts to approach the lower bound declared by Shannon's source coding theorem, which states that any lossless data compression method ...
that represents a
natural number
In mathematics, the natural numbers are the numbers 0, 1, 2, 3, and so on, possibly excluding 0. Some start counting with 0, defining the natural numbers as the non-negative integers , while others start with 1, defining them as the positive in ...
, ''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
A thermometer is a device that measures temperature (the hotness or coldness of an object) or temperature gradient (the rates of change of temperature in space). A thermometer has two important elements: (1) a temperature sensor (e.g. the bulb ...
that gets taller or shorter as ''n'' gets bigger or smaller, and so is sometimes called thermometer code.
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 coding is an ''optimally efficient'' encoding for the following discrete
probability distribution
In probability theory and statistics, a probability distribution is a Function (mathematics), function that gives the probabilities of occurrence of possible events for an Experiment (probability theory), experiment. It is a mathematical descri ...
:
for
.
In symbol-by-symbol coding, it is optimal for any
geometric distribution
In probability theory and statistics, the geometric distribution is either one of two discrete probability distributions:
* The probability distribution of the number X of Bernoulli trials needed to get one success, supported on \mathbb = \;
* T ...
:
for which ''k'' ≥ φ = 1.61803398879..., the
golden ratio
In mathematics, two quantities are in the golden ratio if their ratio is the same as the ratio of their summation, sum to the larger of the two quantities. Expressed algebraically, for quantities and with , is in a golden ratio to if
\fr ...
, 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
Golomb coding is a lossless data compression method using a family of data compression codes invented by Solomon W. Golomb in the 1960s. Alphabets following a geometric distribution will have a Golomb code as an optimal prefix code, making ...
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
Arithmetic coding (AC) is a form of entropy encoding used in lossless data compression. Normally, a String (computer science), string of characters is represented using a fixed number of bits per character, as in the American Standard Code for In ...
performs better for general probability distributions, as in the last case above.
Unary coding is both a
prefix-free code
A prefix code is a type of code system distinguished by its possession of the prefix property, which requires that there is no whole code word in the system that is a prefix (initial segment) of any other code word in the system. It is trivially t ...
and a
self-synchronizing code
In coding theory, especially in telecommunications, a self-synchronizing code is a uniquely decodable code in which the symbol stream formed by a portion of one code word, or by the overlapped portion of any two adjacent code words, is not a ...
.
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
UTF-8 is a character encoding standard used for electronic communication. Defined by the Unicode Standard, the name is derived from ''Unicode Transformation Format 8-bit''. Almost every webpage is transmitted as UTF-8.
UTF-8 supports all 1,112,0 ...
, 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
Bird vocalization includes both bird calls and bird songs. In non-technical use, bird songs (often simply ''birdsong'') are the bird sounds that are melodious to the human ear. In ornithology and birding, songs (relatively complex vocalization ...
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''.
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
A prefix code is a type of code system distinguished by its possession of the prefix property, which requires that there is no whole Code word (communication), code word in the system that is a prefix (computer science), prefix (initial segment) of ...
and is not instantaneously decodable
need look-ahead to decode
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
The following symmetric unary codes can be read and instantaneously decoded in either direction:
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. The largest ''n'' numerical '0' or '-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'.
Canonical codes ca
require less processing time to decodewhen 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.
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.
See also
*
Unary numeral system
The unary numeral system is the simplest numeral system to represent natural numbers: to represent a number ''N'', a symbol representing 1 is repeated ''N'' times.
In the unary system, the number 0 (zero) is represented by the empty string, tha ...
Notes
References
{{Compression Methods
Coding theory
Entropy coding
Data compression