Shannon Entropy Calculator
Inputs
| Text | hello |
|---|---|
| Logarithm base | Bits (base 2) |
Shannon Entropy Calculator
Calculate the Shannon entropy of any text or symbol sequence. Enter a string to see how many bits, nats, or hartleys of information each symbol carries on average.
Inputs
Input
Results
Enter a value to see results.
Entropy
Statistics
Shannon Entropy
Shannon entropy is a measure of the average information content, or unpredictability, of a sequence of symbols. Given a string of text, it answers the question: how many bits (or nats, or hartleys) of information does each character carry on average? The concept was introduced by Claude Shannon in 1948 as the foundation of information theory and remains central to data compression, cryptography, and machine learning.
The entropy formula
For a source that emits symbols from an alphabet of size , where symbol occurs with probability , the Shannon entropy is:
The base of the logarithm determines the unit: base 2 gives bits, base gives nats, and base 10 gives hartleys (also called bans or dits).
Worked example — "hello"
The string "hello" contains five characters: h, e, l, l, o. Character frequencies:
| Character | Count | Probability |
|---|---|---|
| h | 1 | 1/5 = 0.2 |
| e | 1 | 1/5 = 0.2 |
| l | 2 | 2/5 = 0.4 |
| o | 1 | 1/5 = 0.2 |
Applying the formula in bits (base 2):
H=−(3×0.2×log20.2+0.4×log20.4)=−(3×0.2×(−2.3219)+0.4×(−1.3219))≈1.9219 bits per symbolThe total entropy for the full string is bits. This is the theoretical minimum number of bits needed to losslessly encode "hello" using an optimal code for this particular frequency distribution.
Why the formula works
Each term is the contribution of symbol to the total uncertainty. Symbols that appear rarely (small ) carry a lot of information when they do appear — they are surprising. Symbols that appear constantly (large ) carry little information — they are expected.
Shannon showed that entropy is the unique function satisfying three intuitive axioms: it is continuous in the probabilities, it is maximised by a uniform distribution, and adding a certainly-occurring symbol (probability 1) does not change it. Together, these axioms pin down as the only correct measure of average information.
Maximum and minimum entropy
For a source with distinct symbols:
- Maximum entropy: bits per symbol, achieved when all symbols are equally probable.
- Minimum entropy: , achieved when one symbol occurs with probability 1 (no uncertainty).
Actual English text sits around 1–1.5 bits per letter — far below the theoretical maximum of bits — because of the highly non-uniform frequencies of letters and the strong statistical dependencies between adjacent characters (q is almost always followed by u, for example).
Entropy and data compression
Shannon's source coding theorem proves that no lossless compression algorithm can compress a message to fewer bits per symbol than its entropy. Entropy is therefore a hard lower bound on compressed file size.
This connection explains why:
- Files with repetitive content (like a log file full of similar lines) compress well — their entropy is low.
- Truly random or already-encrypted data cannot be compressed — it is already at its entropy limit.
- Compression algorithms such as Huffman coding and arithmetic coding approach the entropy bound, with Huffman coding guaranteed to use at most one extra bit per symbol beyond the bound.
Relation to password strength
Password entropy, as used in security analysis, is a related but distinct concept. It measures the uncertainty from an attacker's perspective, assuming the password is drawn uniformly at random from a pool of possible strings. The formula is , where is the password length and is the character pool size.
Shannon entropy of a specific password — as this calculator computes — measures the frequency distribution of the characters in that one string. A password like "aaaa" has zero Shannon entropy but is still four characters long. For password security, use the Password Entropy Calculator which models the attacker's search space.
Frequently Asked Questions (FAQ)
What does high entropy mean?
High entropy means the symbol frequencies are close to uniform — each character is roughly equally likely, so each one carries more information. A completely random string of 256 distinct ASCII characters has maximum entropy (8 bits per character), because knowing the previous characters tells you nothing about the next one.
Low entropy means the distribution is skewed — some characters dominate, making the string predictable. "aaaa" has zero entropy because every character is certain.
What is the maximum entropy for a string with N distinct symbols?
The maximum Shannon entropy for a source with N distinct symbols is log₂(N) bits per symbol. This maximum is reached only when all N symbols occur with equal probability (a uniform distribution).
For example: a binary string (N = 2) can carry at most 1 bit per symbol; a string drawn from the full 26-letter English alphabet (N = 26) can carry at most log₂(26) ≈ 4.7 bits per symbol. Actual English text sits around 1 to 1.5 bits per letter because of the highly non-uniform letter and word frequencies.
What does entropy have to do with data compression?
Shannon's source coding theorem states that no lossless compression scheme can compress a sequence to fewer bits per symbol than its entropy. The entropy is a theoretical lower bound.
A string with entropy H bits per symbol can in principle be compressed to H bits per symbol but no further. That is why highly compressible files (such as large blocks of repeated text) have low entropy, while truly random or encrypted data — which is already at its entropy limit — cannot be compressed meaningfully.
What is the difference between bits, nats, and hartleys?
All three measure the same underlying quantity (information content) using different logarithm bases:
- Bits (base 2): the natural unit for binary computing. One bit is the information in a fair coin flip.
- Nats (base e ≈ 2.718): used in information theory mathematics and statistical mechanics because the natural logarithm simplifies many formulas.
- Hartleys (base 10): also called bans or dits. One hartley is the information in a choice among ten equally likely outcomes.
To convert: 1 hartley ≈ 3.322 bits ≈ 2.303 nats.