Skip to content

Heat and Entropy

Entropy and the second law

Why time has a direction, and why codecs care

A hot coffee cooling to room temperature is so ordinary you never question it. But watch it backwards: a lukewarm coffee spontaneously pulling heat out of the room to reheat itself, the mug growing warm as the air chills. Every physics law you have met so far would allow it - energy would still be conserved. Yet it never happens, not once in the history of the universe. Something beyond energy is choosing time's direction, and it is the deepest idea in this whole course.

Predict first: is un-mixing forbidden?

Imagine a drop of ink spreading through a glass of water until it is uniformly gray. Now suppose you could reverse every water and ink molecule's velocity at one instant, perfectly. Predict: would the ink gather itself back into a single drop, or is that flatly against the laws of physics? Hold your answer. The surprising truth is that it is not forbidden at all - it is merely so astronomically unlikely that you would wait many lifetimes of the universe to see it once.

Play: watch order dissolve, then try to rewind it

Every particle in this box starts crammed into the left half. Press play. Nothing in the code pulls them rightward - they just bounce and wander - yet they spread to fill the whole box, and the entropy trace underneath climbs and then sits at a ceiling. When it is well mixed, hit "reverse time" and watch what the microscopic laws actually permit.

Every particle starts on the left. Press play: nothing pulls them right, yet they spread, because there are vastly more arrangements that look spread out. The trace is entropy S = ln W climbing to its ceiling. Reverse time and it un-mixes - allowed, but it needs every velocity flipped at once.
on the left
160 / 160
arrangements W
1
entropy S = ln W
0.0
S of max
0%

Two lessons live in that widget. First, spreading is not caused by any force - it happens because there are vastly more ways to be spread out than bunched up, so a wandering system stumbles into spread-out arrangements far more often. Second, reverse-time really does un-mix the gas for a while: the laws are reversible. It only looks impossible because reversing reality would mean flipping every molecule's velocity at the exact same instant, and the faintest error sends it spreading again.

Entropy is a count of arrangements

Here is the move that makes everything click. Separate two ideas. A is the full molecular detail. A is the blurry big-picture view. The key quantity is the multiplicity : how many microstates look the same at the macro level.

Count it for the box. "All 160 particles on the left" is one very special macrostate - there is essentially one way to arrange it. "Roughly half on each side" can be built an astronomical number of ways, because it does not matter which particles are where. In the widget that count is exactly , and it is largest at the even split - the arrangements-W readout jumps from a handful to something like as the gas mixes. is defined as the logarithm of that count:

Stated (Boltzmann): entropy is Boltzmann's constant times the natural log of the number of microstates. It is carved on his gravestone.

Why the logarithm? Two reasons, and both will feel familiar if you have met logs before in exponentials and logarithms. It tames those absurd counts into manageable numbers, and it makes entropy add up: put two independent systems together and their arrangement counts multiply, so their logs - their entropies - add. Entropy is the log of possibility.

The second law is just counting, not a force

The sounds like a cosmic decree. It is really a statement about odds. A system rattling randomly through its microstates spends almost all its time in the macrostates that have the most microstates, simply because there are so many more of them. Order does not decay because a law pushes it to; it decays because disordered arrangements outnumber ordered ones by factors like to one. The arrow of time is a landslide of probability.[1]

This is why the coffee never reheats. For it to un-cool, its slow molecules would have to happen to collect all the fast air molecules' energy - one specific, rare arrangement - when there are overwhelmingly more arrangements where the energy stays spread out. And it is why no engine can be perfect: turn heat into work and you are trying to herd spread-out energy back into organized motion, which the counting forbids you from doing completely. The best possible efficiency, the , is , and it is stated, not derived here - but it falls straight out of entropy bookkeeping.[2]

The same math runs your compression codec

Now the payoff a programmer waits for. Look again at : entropy is a logarithm of a count of possibilities. In information theory you met a quantity that is the same shape:

Shannon entropy: the average surprise of a source, in bits when the log is base 2.

These are not cousins; they are the same idea in two costumes. When arrangements are all equally likely, each has probability , and Shannon's formula collapses to - a plain log of the count of possibilities, exactly like Boltzmann's, differing only in the base of the log and the constant out front. Ludwig Boltzmann counted molecular arrangements in the 1870s; Claude Shannon counted message arrangements in 1948 and arrived at the identical mathematics.[3]

Why a random file cannot be compressed

A compression tool wins by exploiting redundancy: patterns and repeats it can describe more briefly. A perfectly random file has no patterns - every bit is a genuine coin flip, so its information entropy is maximal, the same way a fully mixed gas has maximal thermodynamic entropy. There is nothing to predict and nothing to shorten, so no compressor can shrink it. "Incompressible" and "maximum entropy" are the same statement. This is an informational second law: you cannot squeeze order out of something that has none.
import math

def entropy_bits(probabilities):
    # Shannon entropy H = -sum p log2 p, the average surprise in bits.
    return -sum(p * math.log2(p) for p in probabilities if p > 0)

# For W equally likely arrangements, each has p = 1/W, and the sum collapses
# to a plain log of the count: 4 equal options -> exactly 2 bits.
print(entropy_bits([0.25, 0.25, 0.25, 0.25]))   # 2.0 == log2(4)

# The thermodynamic version is the SAME log of a count, scaled by k:
W = 10**12                                       # microstates in a macrostate
k = 1.380649e-23                                 # Boltzmann constant, J/K
S = k * math.log(W)                              # Boltzmann's S = k ln W
print(S)                                         # joules per kelvin
Two entropies, one function. Boltzmann's log-of-a-count is Shannon's formula for a uniform distribution.

Lock it in

  • Entropy is not "messiness". It is : the log of the number of microscopic arrangements that match the bulk macrostate.
  • The second law is statistics, not a force. Systems drift toward macrostates with vastly more arrangements simply because there are so many more of them.
  • Coffee never reheats and no engine is perfect because both would require herding spread-out energy back into one rare arrangement, against overwhelming odds.
  • Boltzmann's and Shannon's are the same math - both the log of a count of possibilities. For equally likely options, .
  • A perfectly random file is incompressible because its information entropy is maximal - no redundancy to exploit. That is the second law wearing an information-theory hat.

Check yourself

Why does a hot coffee never spontaneously reheat itself by pulling energy back out of the room?

The whole lesson in one question - answer with counting, not with 'energy runs out'. Try to state it, then check.

Entropy is best described as...

A perfectly random file cannot be compressed because...

Match each idea to where it lives.

drop here

entropy in thermodynamics

drop here

entropy in information theory

drop here

what both formulas secretly are

drop here

the second law of thermodynamics

Primary source

Claude Shannon, A Mathematical Theory of Communication (1948)

The paper that founded information theory and named entropy for messages. Read the section where Shannon derives and notes it is the same form physicists already used for thermodynamic entropy - the bridge this lesson is built on. Pair it with Feynman Vol I Ch 46 for the physical side.

Sources

  1. 1.Feynman Lectures Vol I Ch 46, Ratchet and Pawl (irreversibility)
  2. 2.OpenStax University Physics Vol 2, Ch 4: The Second Law of Thermodynamics
  3. 3.Claude Shannon, A Mathematical Theory of Communication (1948)