7. Arithmetic Coding

Encoding with fractional bits

Recall from Huffman coding: each symbol gets an integer number of bits — 1, 2, 3, and so on. But what if a symbol's ideal code length is 0.3 bits?

Consider a symbol with probability 90%. Information theory says its ideal cost is −log₂(0.9) ≈ 0.15 bits. But Huffman coding must use at least 1 bit per symbol. That's 6.7× more than theoretically necessary!

Arithmetic coding breaks through this barrier by encoding the entire message as a single number — not one code per symbol. This lets it achieve fractional bits per symbol and reach the theoretical optimum.

The Core Idea: Narrowing an Interval

Imagine the number line from 0 to 1. Here's the process:

  1. Divide [0, 1) into segments by symbol probability

    Each symbol gets a segment proportional to its probability.

  2. For each symbol in the message, narrow the interval

    Zoom into the sub-segment corresponding to that symbol.

  3. The final interval uniquely identifies the message

    Any number inside the final interval can be used as the compressed representation.

Full Worked Example

Setup

Alphabet with probabilities:

Symbol Probability Interval
A 0.7 [0, 0.7)
B 0.2 [0.7, 0.9)
C 0.1 [0.9, 1.0)

Message to encode: AABCA

At each step, we have a current interval [low, high). We subdivide it according to the symbol probabilities and select the sub-interval for the current symbol.

The formulas:

range = high - low
new_low  = low + range × cumulative_prob_below_symbol
new_high = low + range × cumulative_prob_through_symbol

Step-by-step encoding

Step Symbol Before: [low, high) range After: [low, high)
Start — — — [0, 1)
1 A [0, 1) 1.0 [0, 0.7) — A occupies [0, 0.7) of [0, 1)
2 A [0, 0.7) 0.7 [0, 0.49) — A occupies [0, 0.7×0.7) = [0, 0.49)
3 B [0, 0.49) 0.49 [0.343, 0.441) — B: low + 0.49×0.7 = 0.343, low + 0.49×0.9 = 0.441
4 C [0.343, 0.441) 0.098 [0.4312, 0.441) — C: 0.343 + 0.098×0.9 = 0.4312, 0.343 + 0.098×1.0 = 0.441
5 A [0.4312, 0.441) 0.0098 [0.4312, 0.43806) — A: 0.4312 + 0.0098×0 = 0.4312, 0.4312 + 0.0098×0.7 = 0.43806

Final interval: [0.4312, 0.43806)

Any number in this interval represents the message "AABCA". We could pick, say, 0.435 as our compressed output.

The width tells us the cost

The final interval has width 0.43806 − 0.4312 = 0.00686. This equals the product of all symbol probabilities: 0.7 × 0.7 × 0.2 × 0.1 × 0.7 = 0.00686.

The number of bits needed to specify a point in an interval of width w is approximately −log₂(w) = −log₂(0.00686) ≈ 7.19 bits.

This is the Shannon entropy of the message: −(3×log₂(0.7) + 1×log₂(0.2) + 1×log₂(0.1)) ≈ 7.19 bits. Optimal!

Decoding

Given the number 0.435, the symbol probabilities, and the message length (5), reconstruct the message:

Decoding 0.435

  1. Current interval: [0, 1). Where does 0.435 fall?
    A = [0, 0.7), B = [0.7, 0.9), C = [0.9, 1.0)
    0.435 is in [0, 0.7) → A. Narrow to [0, 0.7).
  2. Subdivide [0, 0.7): A = [0, 0.49), B = [0.49, 0.63), C = [0.63, 0.7)
    0.435 is in [0, 0.49) → A. Narrow to [0, 0.49).
  3. Subdivide [0, 0.49): A = [0, 0.343), B = [0.343, 0.441), C = [0.441, 0.49)
    0.435 is in [0.343, 0.441) → B. Narrow to [0.343, 0.441).
  4. Subdivide [0.343, 0.441): A = [0.343, 0.4116), B = [0.4116, 0.4312), C = [0.4312, 0.441)
    0.435 is in [0.4312, 0.441) → C. Narrow to [0.4312, 0.441).
  5. Subdivide [0.4312, 0.441): A = [0.4312, 0.43806), ...
    0.435 is in [0.4312, 0.43806) → A.

Decoded: AABCA ✓

Why This Achieves Optimal Compression

The final interval's width equals the product of all symbol probabilities. The number of bits needed to uniquely identify a point in that interval is:

bits ≈ −log₂(p₁ × p₂ × ... × pₙ)
     = −Σ log₂(pᵢ)
     = Σ (−log₂(pᵢ))       ← each symbol contributes its ideal cost

This is exactly the Shannon entropy of the message — the theoretical minimum number of bits. Arithmetic coding achieves it (plus at most 1–2 bits of overhead for the entire message, not per symbol).

Huffman coding

Each symbol: integer bits
Waste from rounding: up to 1 bit per symbol
Worst case: 1 bit for a 99% symbol

Arithmetic coding

Entire message: one number
Waste: 1–2 bits total
A 99% symbol costs ~0.014 bits

Practical Implementation Challenges

The precision problem

In our example, the interval shrank to width 0.00686 after just 5 symbols. After thousands of symbols, the interval would be astronomically narrow — far beyond what any floating-point number can represent. Real implementations cannot use floating point.

The solution: integer arithmetic with renormalization

Instead of tracking [low, high) as decimals, use large integers (e.g., 32-bit). When the interval narrows enough that the leading bits of low and high agree, output those bits and shift:

Renormalization

  • If both low and high start with 0: output 0, shift both left (zoom in on the [0, 0.5) half)
  • If both start with 1: output 1, shift both left (zoom in on the [0.5, 1) half)
  • If low starts with 01 and high starts with 10 (straddling 0.5): remember this with a "pending" counter, shift the middle bits. Output the pending bits later when the ambiguity resolves.

This keeps the interval within a manageable range while streaming out bits incrementally. No infinite precision needed!

Adaptive Arithmetic Coding

A powerful feature: you don't need to know the symbol probabilities in advance. Adaptive arithmetic coding starts with uniform probabilities (or some prior) and updates them as each symbol is processed:

  1. Start with assumed probabilities (e.g., all symbols equally likely)
  2. Encode/decode symbol using current probabilities
  3. Update probabilities based on observed frequencies
  4. Both encoder and decoder make the same updates — they stay in sync

The model/coder separation

Arithmetic coding cleanly separates two concerns: the model (which predicts the probability of the next symbol) and the coder (which turns those probabilities into bits). Any model can be plugged in — simple frequency counting, Markov models, neural networks, anything. The coder doesn't care where the probabilities come from.

This separation is one of the most important ideas in compression. Modern compressors are largely about building better models — the coding is a solved problem.

Arithmetic Coding vs Huffman Coding

Huffman Arithmetic
Compression Near-optimal; wastes up to 1 bit/symbol Optimal; wastes ~1 bit total
Speed Fast (table lookups) Slower (multiply + shift per symbol)
Complexity Simple to implement Tricky (precision, renormalization)
Adaptivity Possible but requires rebuilding tree Natural — just update probabilities
Patents None IBM held patents (now expired)
Best for Balanced distributions, speed-critical Skewed distributions, maximum compression

A practical variant called range coding is nearly identical to arithmetic coding but processes whole bytes instead of individual bits, making it faster. LZMA (used in 7-Zip and .xz) uses range coding.

Where Arithmetic Coding Is Used

Format / System Details
H.264 / H.265 video CABAC (Context-Adaptive Binary Arithmetic Coding) — arithmetic coding with context modeling for video bitstream
JPEG 2000 MQ coder — a binary arithmetic coder
LZMA / 7-Zip / .xz Range coding (arithmetic coding variant) for entropy coding after LZ matching
JBIG / JBIG2 Document image compression (fax, scanning)
VP8/VP9/AV1 Boolean arithmetic coding for video frames

Try It Yourself — Arithmetic Coding

Type a message to see arithmetic coding narrow the [0, 1) interval step by step. The probability model is built from the character frequencies in your input. Switch to Decode mode to watch the code point get decoded back into the original message. Try skewed inputs like AAAAAAB to see how arithmetic coding handles highly unequal probabilities.

Probability Model

Encoding Steps

Result

Statistics

Key Takeaways

  • Arithmetic coding represents an entire message as a single number in [0, 1)
  • Each symbol narrows the interval proportionally to its probability
  • Achieves theoretically optimal compression — no rounding waste per symbol
  • Overcomes Huffman's limitation of integer-bit codes
  • Practical implementations use integer arithmetic with renormalization to avoid precision issues
  • The model/coder separation is a fundamental principle: the coder turns probabilities into bits; the model provides the probabilities
  • Used in video coding (H.264, H.265, AV1), JPEG 2000, and LZMA

Arithmetic coding is optimal but slow — it requires a multiply and shift per symbol, and it produces bits sequentially (hard to parallelize). What if we could get the same optimality at the speed of a table lookup? That's ANS — the breakthrough behind Zstandard, the modern compression standard.