On this page
Every time you zip a file, you run into a limit worked out in 1948. In "A Mathematical Theory of Communication", Claude Shannon defined one number that tells you how much information a random source produces and how many bits you need, on average, to record it (Shannon, 1948). You can work it out with a calculator and school logarithms.
What is Shannon entropy?
Shannon entropy is H = −Σ pᵢ log₂ pᵢ, summed over every possible outcome with probability pᵢ (Shannon's paper, Section 6). With logarithms to base 2 the unit is the bit, a word Shannon credits to J. W. Tukey.
The term log₂(1/p) is the surprise, or information content, of one outcome: low when p is close to 1, and high when p is close to 0 (Wikipedia: Entropy (information theory)). Entropy is that surprise averaged over all outcomes, each weighted by how often it happens. So H answers a simple question: how surprised do you expect to be?
How do you calculate Shannon entropy?
Fair coin. Heads and tails each have p = 1/2, so each carries log₂ 2 = 1 bit of surprise, and H = ½ × 1 + ½ × 1 = 1 bit, exactly the figure the Wikipedia entropy article gives for a fair coin.
Biased coin, 90:10. Heads carries log₂(1/0.9) ≈ 0.152 bits; tails carries log₂ 10 ≈ 3.32 bits. Tails is a big surprise, but it is rare. H = 0.9 × 0.152 + 0.1 × 3.32 ≈ 0.137 + 0.332 ≈ 0.47 bits. Shannon's own Figure 7 plots this two-outcome curve, which peaks at 1 bit when p = 1/2 (Shannon, 1948).
Fair die. Six faces, each with p = 1/6, give H = log₂ 6 ≈ 2.585 bits. A die is harder to predict than a coin, so each throw carries more information.
Why is entropy highest when outcomes are equally likely?
Shannon listed the properties that make H a sensible measure of uncertainty (Shannon, 1948). H = 0 only when one outcome is certain. For n outcomes, H is largest, equal to log n, when all the probabilities are equal, which he called "intuitively the most uncertain situation". Making the probabilities more equal increases H. That is why the 90:10 coin scores below 1 bit: you can half-guess the result before it lands.
What does entropy have to do with Huffman coding?
Here entropy becomes a hard limit. Stanford's information theory course notes prove that the average codeword length of any uniquely decodable code is at least H (Stanford EE376A, Lecture 6). The same notes show that a Shannon code averages at most H + 1 bits, and state that the Huffman code, built by repeatedly merging the two least likely symbols, is an optimal prefix code, so its average length sits between H and H + 1. Huffman published the method in 1952 (Proceedings of the IRE).
Try it on the fair die. Huffman's merges give two faces 2-bit codes and four faces 3-bit codes, an average of (2 × 2 + 4 × 3) / 6 ≈ 2.67 bits, just above the entropy of 2.585 bits.
The biased coin shows the catch: one flip still needs a whole bit, far above 0.47. Shannon's fix is to code blocks of symbols, so that the bits per symbol approach H as the blocks get longer (Shannon, 1948, Section 9). We built Huffman codes for the 90:10 coin to see it happen:
One flip at a time: codes 0 and 1, so 1 bit per flip.
Pairs (HH 0.81, HT 0.09, TH 0.09, TT 0.01): code lengths 1, 2, 3 and 3, averaging 1.29 bits per pair, about 0.645 bits per flip.
Triples (HHH 0.729 down to TTT 0.001): code lengths 1, 3, 3, 3, 5, 5, 5 and 5, averaging 1.598 bits per triple, about 0.533 bits per flip.
Each step moves closer to the 0.47-bit limit without going below it.
How can you practise entropy in one session?
Compute H for a coin with p = 0.5, 0.7, 0.9 and 0.99, then sketch the curve.
Check your 0.99 answer against the 0.081 bits in the Stanford EE376A notes.
Build a Huffman code for four letters with probabilities 1/2, 1/4, 1/8 and 1/8. Both H and the average code length come to exactly 1.75 bits, because every probability is a power of 1/2.
Explain to a friend, without the formula, why a die carries more bits than a coin.
If logarithms or weighted averages trip you up, the Fermi Concept Library covers every concept in physics, chemistry, biology, mathematics and computing, each with its own lesson. If your Huffman tree keeps coming out wrong, Fermi's AI tutor notices where you are stuck and decides whether to explain, ask or show.
Start with the coin: once 0.47 bits makes sense, the rest follows.



