ArticleslgStudy

science

Unary coding

Unary coding is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Unary coding rather than just read about it. In short: Unary coding, 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 definiti…

Key takeaways

  • Unary coding belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Unary coding to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Unary coding from memory before moving on to harder problems.

Reference excerpt

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

P ⁡ ( n ) = 2 − n {\displaystyle \operatorname {P} (n)=2^{-n}\,}

for n = 1 , 2 , 3 , . . . {\displaystyle n=1,2,3,...} . In symbol-by-symbol coding, it is optimal for any geometric distribution

P ⁡ ( n ) = ( k − 1 ) k − n {\displaystyle \operatorname {P} (n)=(k-1)k^{-n}\,}

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

P ⁡ ( n ) ≥ P ⁡ ( n + 1 ) + P ⁡ ( n + 2 ) {\displaystyle \operatorname {P} (n)\geq \operatorname {P} (n+1)+\operatorname {P} (n+2)\,}

for n = 1 , 2 , 3 , . . . {\displaystyle 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 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. 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 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:

These codes have a unique property wherein if one were to scan data ( 1s and 0s ) of any probability distribution for symmetric unary codes, the average length of the scanned symmetric unary code is always around 2. This can help with timing concerns as almost every protocol has to put in checks and balances for continuous streams of low entropy data which can damage components like USB peripherals etc.

Canonical unary codes For unary values where the maximum length is known, one can use canonical unary codes that are of a somewhat numerical nature and different from character based codes. The largest length n is known, numerical 0 ( 2 n − 1 {\displaystyle \operatorname {2} ^{n}-1\,} in bijective ) or -1 ( 2 2 n − 2 {\displaystyle \operatorname {2} ^{2n}-2\,} in bijective ) is assigned as the boundary condition equivalent to repeating a digit the maximum 'n' number of times, then for each step reducing the number of digits by one and increasing/decreasing the result by numerical '1'.

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Unary coding

Start with the simplest possible case. Write down what Unary coding claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Unary coding before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Unary coding ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Unary coding

In research
Unary coding appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Unary coding in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Unary coding is common in secondary-school and first-year university syllabi. It links to neighbouring topics Coding theory, Data compression, Entropy coding, so understanding it makes those chapters shorter.
In everyday life
Look for Unary coding outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Unary coding” →

Affiliate

Preply — study more efficiently by working with a personal tutor. 50% off.

How to study Unary coding in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Unary coding means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Unary coding out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Unary coding in simple terms?

Unary coding, 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 positi…

Why does Unary coding matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Unary coding?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Unary coding.

Tags

  • Coding theory
  • Data compression
  • Entropy coding

Keep exploring