On this page
If you have read Shannon entropy and why a fair coin carries 1 bit, the next question is practical: how short can a code be? The Kraft inequality says which codeword lengths are possible, and the source coding theorem gives the best average.
What is the Kraft inequality?
A prefix-free code (prefix code) is one where no codeword is the start of another, so a stream of bits can be read left to right without separators. For a binary prefix code with codeword lengths ℓ₁, ℓ₂, …, ℓₙ, the Kraft inequality states that Σ 2^(−ℓᵢ) ≤ 1 (Kraft–McMillan inequality).
Leon Gordon Kraft stated it for prefix codes in his 1949 MIT master's thesis in electrical engineering (MIT DSpace record). McMillan showed in 1956 that the same inequality holds for every uniquely decodable code, prefix-free or not (McMillan, IEEE Transactions on Information Theory). That is why it is often called the Kraft–McMillan inequality.
Read the sum as a budget: a codeword of length 1 spends half, length 2 a quarter. You cannot overspend.
How does the binary-tree proof work?
Draw every binary string as a node in a tree: go left for 0, right for 1. A codeword of length ℓ is a node at depth ℓ, and prefix-free means no codeword sits below another.
Let L be the longest codeword length. The tree has 2^L nodes at depth L.
A codeword at depth ℓᵢ has exactly 2^(L−ℓᵢ) descendants at depth L (counting itself when ℓᵢ = L).
Because no codeword lies below another, these sets of descendants never overlap.
So Σ 2^(L−ℓᵢ) ≤ 2^L. Divide by 2^L and you get Σ 2^(−ℓᵢ) ≤ 1.
The diagram at the top shows this for the code 0, 10, 110, 111 with L = 3: the four codewords claim 4, 2, 1 and 1 of the 8 bottom nodes, the whole row, so the sum is exactly 1.
How do you build a prefix code from given lengths?
The converse also holds: if your lengths satisfy the inequality, a prefix code with exactly those lengths exists (Kraft–McMillan inequality). Here is a construction you can do by hand.
Sort the lengths from shortest to longest.
For codeword i, add up 2^(−ℓⱼ) over all earlier codewords j. Call this running total cᵢ, starting from c₁ = 0.
Write cᵢ in binary and take the first ℓᵢ bits after the point. That is codeword i.
For lengths 1, 2, 3, 3 the running totals are 0, 1/2, 3/4 and 7/8, which in binary are 0.000, 0.100, 0.110 and 0.111. Taking 1, 2, 3 and 3 bits gives 0, 10, 110 and 111. Each running total lies past the interval claimed by earlier codewords, so none is a prefix of another.
Why can't average code length beat the entropy?
The bound goes back to C. E. Shannon's 1948 paper, A Mathematical Theory of Communication (Shannon, Bell System Technical Journal). For symbol codes, the source coding theorem says an optimal uniquely decodable binary code has expected length L̄ with H ≤ L̄ < H + 1, where H = −Σ pᵢ log₂ pᵢ is the entropy in bits (source coding theorem).
The lower bound follows from Kraft in three lines:
L̄ − H = Σ pᵢ ℓᵢ + Σ pᵢ log₂ pᵢ = −Σ pᵢ log₂(2^(−ℓᵢ)/pᵢ).
Because log is concave (Jensen's inequality), this is at least −log₂ Σ 2^(−ℓᵢ).
Kraft says Σ 2^(−ℓᵢ) ≤ 1, so −log₂ of it is at least 0. Hence L̄ ≥ H.
Equality needs pᵢ = 2^(−ℓᵢ) for every symbol, that is, every probability a power of 1/2. For the upper bound, choose ℓᵢ = ⌈−log₂ pᵢ⌉: these lengths satisfy Kraft, so a prefix code exists, and each length is less than −log₂ pᵢ + 1, so its average is below H + 1.
Worked example: probabilities 1/2, 1/4, 1/8, 1/8
Take symbols A, B, C, D with these probabilities: all powers of 1/2, the equality case.
Entropy: H = (1/2)(1) + (1/4)(2) + (1/8)(3) + (1/8)(3) = 1.75 bits per symbol.
Ideal lengths: −log₂ pᵢ gives 1, 2, 3, 3. The Kraft sum is 1/2 + 1/4 + 1/8 + 1/8 = 1, so the code exists and wastes no room.
The code: A = 0, B = 10, C = 110, D = 111, from the construction above.
Average length: (1/2)(1) + (1/4)(2) + (1/8)(3) + (1/8)(3) = 1.75 bits, exactly H.
A fixed-length code needs 2 bits for four symbols, so this code saves a quarter of a bit per symbol on average.
Decode 0100110 to check: 0 is A, 10 is B, 0 is A, 110 is C. No commas are needed, and no other reading is possible. For a different way of arranging binary words, see Gray code, the binary reflected code.
Where can you practise prefix codes?
To revisit binary trees and logarithms, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing. If you get stuck on your own length sets, the Fermi Problem Library has a step-by-step solver that explains the reasoning rather than printing an answer.



