Sub topic 1
Why Fixed-Length Codes Waste Bits
Most text and binary data is stored with a fixed-length code. A fixed-length code gives every symbol a bit pattern of the same width. ASCII uses 8 bits per character, and a byte-oriented file uses 8 bits per byte value. Fixed-length codes are simple to read and write, but they spend the same number of bits on a symbol that shows up constantly as on one that shows up once. This chapter measures that waste on a small example.
Prerequisites
This chapter assumes you know:
- Binary numbers and how many values
nbits can represent (2^n) - Basic Python, including dictionaries and
collections.Counter - Logarithms base 2, at the level of "log2(8) = 3"
Binary trees, heaps, and probability come in later chapters.
The cost of a fixed width
If an alphabet has k distinct symbols, a fixed-length code needs ceil(log2(k)) bits per symbol. Take the string abracadabra, which has 11 characters and 5 distinct symbols:
| Encoding | Bits per symbol | Total bits |
|---|---|---|
| ASCII | 8 | 88 |
| Fixed-length for 5 symbols | 3 | 33 |
Using 3 bits instead of 8 already saves a lot, because the code covers only the symbols that actually appear. It still wastes bits in two ways:
- Three bits can name 8 values, but only 5 are used, so 3 patterns go unused.
- The letter
amakes up 5 of the 11 characters, yet it costs as much asd, which appears once.
Variable-length codes
A variable-length code lets each symbol have a different bit length. Frequent symbols get short codes and rare symbols get long ones. Here is one such code for abracadabra:
| Symbol | Count | Code | Bits used |
|---|---|---|---|
| a | 5 | 0 | 5 |
| b | 2 | 10 | 4 |
| r | 2 | 110 | 6 |
| c | 1 | 1110 | 4 |
| d | 1 | 1111 | 4 |
The total is 23 bits, compared with 33 for the fixed 3-bit code. This script checks both numbers:
import math
from collections import Counter
text = "abracadabra"
freq = Counter(text)
fixed_width = max(1, math.ceil(math.log2(len(freq))))
fixed_bits = fixed_width * len(text)
variable_code = {"a": "0", "b": "10", "r": "110", "c": "1110", "d": "1111"}
variable_bits = sum(len(variable_code[ch]) for ch in text)
print("ASCII:", 8 * len(text), "bits")
print("Fixed:", fixed_bits, "bits at", fixed_width, "bits/symbol")
print("Variable:", variable_bits, "bits")
# ASCII: 88 bits
# Fixed: 33 bits at 3 bits/symbol
# Variable: 23 bitsThe catch: where does one code end?
A fixed-length decoder always knows where the next symbol starts: it reads every 3 bits. A variable-length bitstream has no separators, so the code itself has to make the boundaries clear.
Fixed-length codes are only optimal when every symbol is equally likely and the number of symbols is a power of two. Real data, such as English text, source code, and pixel values, is almost never that uniform. Huffman coding takes advantage of the skew.
Exercise
The message "aaaaaabc" has 8 characters. How many bits does a minimal fixed-length code need for it, and how many does the code a = 0, b = 10, c = 11 need?
Show answer
There are 3 distinct symbols, so the fixed code needs ceil(log2 3) = 2 bits per symbol, which gives 16 bits. The variable code uses 6×1 + 1×2 + 1×2 = 10 bits.
Exercise
For which kind of input does a fixed-length code waste no bits compared with any variable-length code?
Show answer
Input where all symbols appear equally often and the number of distinct symbols is a power of two, such as 4 symbols each with a frequency of 25%. Then every symbol deserves the same length, log2(k) bits, and no bit patterns go unused.