AI Glossary / MLOps and deployment / Beginner

Unary coding

Definition

Unary coding represents a nonnegative integer by repeating one bit a number of times equal to the integer and ending with the opposite bit. Using the convention adopted here, n is encoded as n zero bits followed by one: 0 becomes 1, 1 becomes 01 and 3 becomes 0001.

What it means

Unlike ordinary positional binary, unary coding records a number through the length of a run. The terminating bit tells the decoder where that number ends, so several values can be written consecutively without inserting a separate separator character.

This is simple and can suit quantities that are usually very small. It becomes expensive for large values because length grows directly with the value. Unary coding is therefore often useful as one component of a larger compression method rather than as a general replacement for binary integers.

How it works

To encode n, emit n zeros and then a one. To decode, count zeros until the terminating one arrives, return that count and restart for the next value. A codeword has n + 1 bits, so even zero occupies one bit under this convention.

The roles of zero and one can be reversed in other definitions, and some descriptions use positive integers with a shifted count. Encoder and decoder must agree on both the bit convention and the integer range. Those choices are part of the format, not interchangeable notation within a stream.

Unary words are self-delimiting under the agreed convention. However, the complete message still needs framing, such as a known number of values or an enclosing bit length. A decoder also needs a way to handle an unfinished run or a value that exceeds the application's permitted range.

Three practical examples

1. Rice coding: a codec can split an integer into a quotient and a fixed-width remainder, unary-coding the quotient. FLAC uses this kind of construction for folded prediction residuals.

2. Compact counters: a custom format for counts that are overwhelmingly zero or one can test unary representation against fixed-width alternatives. The entire distribution, including rare large counts, determines the result.

3. Bitstream education: a learner can concatenate unary words and decode them by counting runs. The exercise makes the distinction between a value and its encoded length immediately visible.

Think of it this way

Think of tally marks followed by a closing mark: three tallies and a closure represent three, while an immediate closure represents zero. A reader knows when to stop counting.

The analogy is limited because a binary stream has no visual spaces unless the format supplies them. The decoder relies on the terminating bit, and a damaged terminator can change how later bits are grouped into values.

Worked example

Encode the values [0, 2, 1, 4] using zeros followed by one. Their codewords are 1, 001, 01 and 00001. Concatenating them gives 10010100001, with lengths 1 + 3 + 2 + 5 = 11 bits.

If these values were known to lie between 0 and 7, a fixed-width representation would require three bits each, or 12 bits total. Unary saves one bit for this particular sequence, before framing overhead.

Now consider a sequence containing just [0, 0, 0, 7]. Its unary length is 1 + 1 + 1 + 8 = 11 bits as well. In contrast, [7, 7, 7, 7] requires 32 unary bits but still only 12 fixed-width bits. The comparison depends on the values' distribution, not merely on the fact that unary words have variable lengths. In general, expected unary length is E[n] + 1 bits under this convention.

Illustrative case study

Fictional case: CountNest designs a compact record format for small repetition counts. A test batch contains mostly zeros, and unary coding looks attractive. Later batches include occasional long runs whose counts require many more bits than expected.

The team measures both average size and the largest permitted encoded value. It compares unary coding with a quotient-and-remainder scheme and a fixed-width alternative, including framing overhead. It also tests truncated streams and confirms that the decoder stops according to the format's bounds rather than counting indefinitely.

The fictional lesson is that a compact common case is only part of a format decision. Large values, boundaries and malformed input behavior also need explicit treatment.

Common misconceptions and mistakes

Unary coding is not the same as one-hot encoding. One-hot representations normally select one position within a fixed-length vector; unary coding uses a variable-length run to represent magnitude.

Do not omit the terminator when applying the convention in this article. Without it or some external length rule, the boundary between successive values would be unclear.

Nor does self-delimiting mean error-correcting. A flipped or missing bit can change values and boundaries. The enclosing format may need separate integrity or recovery mechanisms, depending on its purpose.

Business use

Businesses rarely choose unary coding as a standalone product feature, but it appears inside codecs and compact data representations. Understanding it helps explain why a poor parameter choice can make an otherwise efficient coder produce unexpectedly large outputs.

When evaluating a format, examine the range and distribution of encoded quantities. Calculate expected size and test outliers. Specify the exact convention, framing and allowed limits in the format documentation, then verify round-trip reconstruction. For AI data pipelines, those details matter when compact records must remain interoperable across tools and versions.

People also ask

How many bits represent zero?

One bit in the convention used here: the terminating one appears immediately, with no preceding zeros.

Is unary better than binary?

It depends on the distribution and framing requirements. Very small values can be cheap, while large values are costly.

Can unary coding represent negative numbers directly?

The basic version described here represents nonnegative integers. Signed values need an agreed mapping or additional convention before applying it.

Check your understanding

how does the stream 0011 decode using zero-run unary coding?

Answer: 001 represents 2, and the following 1 represents 0, so the values are [2, 0]. Reading the same bits as positional binary would be a different interpretation.

Related terms

  • Golomb coding
  • Prefix code
  • One-hot encoding
  • Lossless compression
  • Rice coding
  • self-delimiting code
  • bitstream
  • integer encoding

Learn next

Encode and decode several short sequences, including zeros and a large outlier. Then study Rice coding to see how unary represents a quotient while a binary suffix represents the remainder. Finally, distinguish codeword boundaries from the framing of an entire message.

More in MLOps and deployment

  • Transform coding - Transform coding compresses data by first expressing it in a different mathematical representation, then encoding the resulting coefficients.
  • Trellis quantization - Trellis quantization chooses quantized values by searching paths through a graph of candidate decisions and states.
  • Tunstall coding - Tunstall coding is a lossless variable-to-fixed-length coding method.
  • Uncompressed video - Uncompressed video represents frames directly through their specified image samples rather than a video compression scheme that encodes spatial or temporal...
  • Universal code (data compression) - A universal code in data compression is a coding scheme designed to work across a specified class of sources without requiring the exact source...
  • Variable bitrate - Variable bitrate, or VBR, is an encoding approach in which the amount of data used per unit of media time can change.

Sources

Primary source: RFC 9639, “Free Lossless Audio Codec (FLAC),” conventions and Rice-coded residual sections: https://www.rfc-editor.org/rfc/rfc9639.html . It supplies a concrete zero-run unary convention and codec use. The standalone sequences, size comparisons and CountNest case are original educational examples, not FLAC file encodings.

Previous: Unary code Next: Unary constraint

  • Home
  • Daily AI Updates
  • Blog
  • AIFBL Academy
  • AI Glossary
  • Products and Tools
  • Store
  • Free CFO AI Prompt Pack
  • About Shihan Sheriff
  • Privacy Policy
  • Terms of Use
  • Refund Policy
  • Cookie Policy
  • Earnings Disclaimer