A decipherable code is one whose extension to finite strings by concatenation is injective: an encoded string has a unique sequence of source letters. In particular its codewords are distinct and nonempty. Put and . For strings of exactly source letters, let count the encoded strings of length . Unique decipherability gives , since distinct source strings yield distinct encoded strings. Expanding gives
Taking th roots and letting proves the McMillan inequality
Add to the four specified binary words. None of the resulting seven words is a proper suffix of another: the longer words end in , and their shorter terminal strings never equal another of the specified longer words. Thus this is a suffix code. Reading from the right determines the last codeword uniquely; deleting it and repeating proves unique decipherability. Its length sum is , in agreement with the bound.
A binary uniquely decodable code assigns a finite binary word to each source symbol, so that no two distinct finite sequences of source symbols have the same concatenated encoding. This does not require the stronger prefix property. All codeword lengths are positive.
Put and let count sequences of source symbols whose encoded total length is . Expanding the product gives
Unique decodability makes these encodings distinct, so , the number of binary strings of that length. There are at most possible lengths. Therefore
Taking th roots and letting proves the McMillan inequality:
Order the prescribed lengths as . Construct a prefix code on the binary tree. At depth , previously assigned codewords exclude of the vertices. The Kraft inequality implies
so an available vertex can be assigned to the next symbol. None of the assigned words is a prefix of another. Reading each word up to the next assigned vertex gives unique decoding, so this constructs the required uniquely decodable code.
For completeness the same length inequality is necessary even for a uniquely decodable code which is not a prefix code. Write and . For successive symbols, distinct symbol strings have distinct concatenated codewords. At total length there are at most such strings. Therefore
Taking th roots and letting gives . This is the McMillan inequality.
Set . The relative entropy inequality gives
Here Shannon entropy uses base , with . The relative entropy is nonnegative, with equality precisely when the two probability distributions coincide; this follows from . Consequently equality holds exactly when
In particular all alphabet symbols must have positive probability for equality with finite prescribed lengths.