Visual walkthrough — UDP — header, use cases, checksum
Before line one, two words we must earn:
Step 1 — Why sum anything at all?
WHAT. We take the data we want to protect and chop it into 16-bit words. Our example: three words
WHY. Bits flip in transit — a router glitches, a cable picks up noise, and a 0 silently becomes a 1. We want a cheap way to notice. The idea: squash all the data into one small number (a "fingerprint"). Send the fingerprint too. If any bit flips, the fingerprint changes and won't match. Summing is the cheapest fingerprint a computer can make — addition is one CPU instruction.
PICTURE. Look at the red word: it's the one we'll watch as it flows through every step. The three words are just three rows of switches lined up, waiting to be added.
Here = the first slice, = the second, = the third. Each is a plain integer between and . Nothing magic yet — we're only slicing.
Step 2 — Add the first two words (and hit a wall)
WHAT. Add as ordinary numbers:
WHY. Plain addition is our fingerprint machine. But look — the answer has five hex digits (), and a 16-bit word only holds four (). The leading 1 is a 17th bit that overflowed the row of switches. We can't just throw it away — that would lose information, and a lost bit means a corrupted word could sneak past unnoticed.
PICTURE. The red 1 on the far left is the overflow carry — it fell off the left edge of the 16-switch row. Everything to the right of it () still fits; the red 1 does not.
- = the honest full sum.
- The red 1 (worth ) = the part that won't fit in 16 bits.
- = the part that does fit.
Step 3 — Wrap the carry around (the clever trick)
WHAT. Take the overflow 1 and add it back onto the low 16 bits:
This "add the carry back in" move is what makes it one's-complement addition (as opposed to ordinary addition).
WHY. We refused to discard the carry in Step 2. Recycling it back into the low bits keeps the result inside 16 bits without losing any information. Mathematically, wrapping a carry of back as works because — every overflow is worth exactly +1 in this recycled arithmetic. (This is the One's complement arithmetic fact that makes the whole scheme click.)
PICTURE. The red arrow loops the overflow bit from the far left back to the far right and drops it into the low end. The switches settle to .
- = the wrapped, back-in-range running sum.
- Note the last digit went from
BtoC: the recycled+1landed there.
Step 4 — Add the remaining word
WHAT. Fold in : No overflow this time, so no wrap is needed.
WHY. We repeat Steps 2–3 for every remaining word — add, and wrap if it overflows. Here is zero, so the sum doesn't budge. We keep going until every word has been folded in. What we're left with — — is the complete fingerprint of the data.
PICTURE. Adding a row of all-off switches () changes nothing; the red running-sum bar stays put. This is the "no carry" branch of the loop.
- = the final sum over all data words.
- Call this quantity . We'll use it in the next step.
Step 5 — Flip the bits to make the checksum
WHAT. Take the one's complement of the sum — flip every switch: This is what actually travels in the UDP header's Checksum field.
WHY send the flip and not itself? Because of one beautiful identity: a number plus its own flip is always all-ones, Every switch that's OFF in is ON in , and vice-versa — so together they light up all 16. This means the receiver won't need to know ; it can just add everything (data plus the checksum) and expect . Sending the flip pre-loads that check.
PICTURE. Top row = . Bottom row = its flip, the red checksum . Every switch in the bottom row is the opposite of the one above it.
- = data fingerprint (stays with the sender's math).
- = the transmitted checksum.
Step 6 — The receiver checks by summing to all-ones
WHAT. The receiver adds up the data words and the checksum, one's-complement style: All ones → accept. Anything else → drop silently.
WHY this works. The receiver re-adds the data to get again, then adds the checksum . By Step 5's identity, . So a clean datagram always sums to all-ones. If even one bit flipped in transit, changes, the sum is no longer all-ones, and the receiver knows. Note: it can detect the error but not fix it — dropping is all UDP does (recovery is the app's job, as the parent note stressed).
PICTURE. Two red bars ( and ) merge into a single all-lit row — the "green light."
- = the pass signal. Any other value = corruption.
Step 7 — Edge case: the sum is genuinely zero
WHAT. Suppose the data really does add up to . Then . Fine — send . But what if the checksum itself computes to ? UDP forbids transmitting a literal as the checksum; you send in its place.
WHY. In one's-complement arithmetic there are two spellings of zero: (all off) and (all on, "negative zero"). They're equal. That freedom lets UDP reserve the exact value to mean "I didn't compute a checksum — don't check me" (allowed in IPv4, forbidden in IPv6). So a real checksum of zero is always written as its twin , keeping unambiguous.
PICTURE. Two switch-rows, all-off and all-on, joined by an = — one labelled "means: no checksum", the other (red) "the value we actually send".
- = reserved flag: checksum disabled.
- = the real "zero" checksum you transmit instead.
The one-picture summary
Everything above is one loop and one flip. Here is the whole machine on a single canvas: slice → add-with-wrap (repeat) → flip → transmit; and on the far side, add-everything → expect all-ones.
Recall Feynman retelling — the whole walkthrough in plain words
I want to protect a message so my friend can tell if it got smudged. First I chop it into equal-size chunks and just add them all up — that's my fingerprint. But addition sometimes overflows past 16 digits; instead of throwing the spillover away (which would lose info), I loop the overflow back around and add it to the bottom — that's the "one's-complement" wrap. Once every chunk is folded in, I have one fingerprint number. Now the trick: instead of sending the fingerprint, I send its photo-negative (every 0↔1 flipped), because a number plus its own negative always lights up all sixteen bits. So my friend just adds everything I sent — the chunks and the negative — and if it comes out all ones, nothing got smudged; if not, they toss it and wait for the next one. One special rule: an all-zero result gets rewritten as all-ones before sending, because plain all-zeros is the secret code for "I skipped the checksum."
Recall Quick self-test
Why do we wrap the carry instead of discarding it? ::: Discarding the overflow bit would lose information, letting some corrupted datagrams pass undetected. Wrapping keeps the result in 16 bits while preserving every bit's contribution (an overflow of 0x10000 is worth +1 in this arithmetic). Why transmit the complement of the sum, not the sum? ::: Because , so the receiver can simply add the data plus the checksum and check for all-ones, without needing to know the sum separately. What does a transmitted checksum of 0x0000 mean? ::: "No checksum was computed" (allowed in IPv4). A genuine zero result is sent as 0xFFFF instead so the two never collide.