encode | decode | visualize

> unary | tally | simple <

// Unary Coding - The simplest integer representation using tally marks

[SIMPLE]

Simplest Code

Most basic encoding - just count with marks.

[VISUAL]

Visual Representation

Easy to understand tally mark visualization.

[VARIANTS]

Multiple Variants

Standard, inverted, and truncated unary codes.

>> technical info

How Unary Coding Works:

Unary coding represents integer n using n identical symbols followed by a terminator. Standard unary uses n ones followed by zero. Inverted uses n zeros followed by one. Truncated unary omits the terminator for the last value in a known range.

Unary Variants:

Standard Unary: 0 → 0 1 → 10 3 → 1110 5 → 111110 Inverted Unary: 0 → 1 1 → 01 3 → 0001 5 → 000001 Truncated (range 0-3): 0 → empty 1 → 1 2 → 11 3 → 111

Why Use Unary Coding:

  • >Component in other codes
  • >Golomb/Rice quotient
  • >Elias coding prefix
  • >Simple to implement
  • >Educational purposes

>> frequently asked questions

What is Unary coding?

Unary coding is the simplest variable-length code, representing integer n as n repetitions of a symbol (typically 1) followed by a different symbol (typically 0) as a terminator. It's like tally marks in binary.

When is Unary efficient?

Unary is only efficient for very small integers or highly skewed distributions where most values are 0 or 1. It uses n+1 bits for integer n, making it very inefficient for large values. It's mainly used as a component in other codes.

What's truncated unary?

Truncated unary is used when the range of values is known. The largest value doesn't need a terminator since its length uniquely identifies it. This saves one bit for the maximum value in the range.

Where is Unary used?

Unary is rarely used alone but is a key component in Golomb/Rice coding (for quotients), Elias Gamma/Delta (for lengths), and other variable-length codes. It's fundamental to many compression algorithms.

Other Languages