The idea: add the words, send the complement

IP, UDP and TCP all carry the same small error check, the Internet checksum (RFC 1071). The sender cuts the bytes into 16-bit words and adds them up. It flips every bit of the sum and sends that as the checksum. The receiver adds the same words plus the checksum. A number plus its complement is all ones, so an undamaged datagram always gives 0xFFFF. Any other result means something changed on the way.

The sender adds twelve 16-bit words of a UDP datagram, with the checksum field at 0, and gets 0x75F8. NOT gives the checksum 0x8A07, which goes in the header. The receiver adds the same twelve words with 0x8A07 in the field and gets 0xFFFF, so it accepts the datagram.
The sender sends NOT of the sum; the receiver's total, checksum included, is 0xFFFF when nothing changed.

One's-complement addition

The words are added with one's-complement arithmetic. It is ordinary 16-bit addition with one change: a carry out of bit 15 is not thrown away. It is added back in at bit 0. This is the end-around carry. For example 0x9727 + 0x7069 = 0x10790, 17 bits. The top 1 wraps around: 0x0790 + 1 = 0x0791 (Demo: sender computes the checksum, word 11).

Because every carry comes back, the order of the additions does not matter, and neither does when the carries are added. A fast implementation adds the words into a 32-bit register and folds the high half into the low half at the end. The receiver on the page does it that way: the twelve words add up to 0x2FFFD, and 0xFFFD + 2 = 0xFFFF. Byte order does not matter either: adding byte-swapped words gives the byte-swapped sum, so a little-endian CPU can add the words as they sit in memory and swap once at the end.

What UDP adds up: the pseudo-header

The UDP checksum (RFC 768) covers more than the UDP header and the data. In front it adds a pseudo-header: the source and destination IP addresses, a zero byte, the protocol number 17 and the UDP length. The pseudo-header is never sent. Both ends build it from the IP header. If a router or a bug delivers the datagram to the wrong address, the receiver's pseudo-header differs and the sum is wrong.

Rows of 32 bits. The pseudo-header: source IP 192.168.1.10, destination IP 192.168.1.20, a zero byte, protocol 17 and UDP length 12. It is marked not sent. Then the UDP header: source port 5000, destination port 7, length 12, checksum. Then the data ping. An odd-length payload such as pin gets one zero pad byte that is added but not sent.
The checksum covers the pseudo-header, the header and the data; only the last two go on the wire.

Two more rules: the checksum field itself counts as 0 while the sender computes it, and data of odd length gets one zero byte at the end for the sum only. The pad byte is not sent (Demo: odd length, pad a zero byte: "pin" adds as 0x7069 and 0x6E00, and the checksum is 0x8A70).

The receiver's check

The receiver builds the same twelve words, this time with the checksum the sender wrote. The sender's words add up to S, and the checksum is NOT S. In one's complement S + NOT S = 0xFFFF, so a clean datagram always gives 0xFFFF (Demo: clean link, receiver sum FFFF). Some code checks it the other way: it takes NOT of the total and expects 0. A datagram that fails is dropped silently. UDP does not resend it; on Linux the InCsumErrors counter in /proc/net/snmp goes up. One flipped bit changes the sum by a power of two, so it is always caught (Demo: one flipped bit is caught: "ping" arrives as "pinG", the sum is 0xFFDF).

Two zeros: 0x0000 means "no checksum"

One's complement has two ways to write zero: 0x0000 and 0xFFFF. UDP over IPv4 uses that. A checksum field of 0x0000 means "the sender did not compute a checksum", and the receiver skips the check (Demo: checksum off, error goes unseen). So when a real checksum comes out as 0x0000, the sender writes 0xFFFF instead. It is the same number in one's complement, and the receiver's sum still comes out right (Demo: checksum 0x0000 is sent as 0xFFFF: from port 40335 the words add to 0xFFFF, and the receiver's plain sum is 0x3FFFC, 0xFFFC + 3 = 0xFFFF).

IPv6 has no header checksum of its own, so it makes the UDP checksum mandatory (RFC 8200). A UDP datagram over IPv6 with a zero checksum is dropped. TCP never had the option: its checksum, with the same pseudo-header idea, is always required.

What it catches, and what it misses

errorInternet checksumCRC-32
one flipped bitalways caughtalways caught
two 16-bit words swappednever caught: addition ignores ordercaught
the same bit flipped 0 → 1 in one word and 1 → 0 in anothernever caught: +2k and −2k cancelcaught
a word changed from 0x0000 to 0xFFFFnever caught: both are zerocaught
a burst of up to 15 bitscaughtcaught (bursts up to 32 bits)
a random larger errormissed about 1 in 65,536missed about 1 in 4 billion

The weak point is that a sum depends only on the values that are added, not on their order or on which word a change landed in (Demo: swapped words slip through: "ping" arrives as "ngpi", Demo: two flips in one column cancel: "ping" arrives as "pkne"; both sums are 0xFFFF). A CRC divides the bits as a polynomial, so each bit is weighted by its position and these errors change the remainder.

Left: the data words 0x7069 0x6E67 (ping) and the swapped 0x6E67 0x7069 (ngpi) give the same one's-complement sum 0xDED0, so the checksum cannot tell them apart. Right: CRC-32 of ping is 0x25D53DFD and of ngpi is 0xAC742283, so a CRC sees the swap.
Swapped words give the same sum, so the checksum is blind to them; a CRC gives a different value.

Why such a weak check is still used

It is cheap. A CPU adds 32 or 64 bits at a time and folds at the end, and the sum can be computed while the data is being copied. A router that lowers the IPv4 TTL can fix the header checksum without adding everything again (incremental update, RFC 1624). Most network cards now compute and check it in hardware (checksum offload). The real protection sits elsewhere: every Ethernet frame carries a CRC-32, and TLS adds a cryptographic check on top. Studies of real traffic still find damaged packets that only the TCP or UDP checksum catches, mostly from bugs in hosts and routers, where the link-layer CRC is recomputed after the damage.

See also TCP vs UDP for what UDP does with the datagram after the check, and Hamming Code for a code that can also correct the bit it finds.

What the page leaves out

The page uses one IPv4 datagram with a 3- or 4-byte payload. It leaves out the IPv4 header checksum (the same sum over the 20-byte header only), the TCP and ICMP checksums, the IPv6 pseudo-header with 128-bit addresses, UDP-Lite's partial coverage, the incremental update formula of RFC 1624, and how checksum offload and NAT rewrite the field.

On the mind map

Open the Networking mind mapNetworkingLayers & links: frames on one local networkLayers & linksIP & routing: packets hop between networksIP & routingCore services: address, name and timeCore servicesTCP, UDP & sockets: end to end between two portsTCP, UDP & socketsHTTP & TLS: what a browser does per requestHTTP & TLSAPIs & push: how apps talk to serversAPIs & pushProxies & edge: boxes in the middle of the pathProxies & edgeContainers: virtual networks from Linux partsContainersReal-time media: voice and video over UDPReal-time mediaSecurity at the edge: filter traffic at the borderSecurity at the edgeProtocol layersProtocol layerseach layer wraps the one above in a headerHub vs switch vs routerHub vs switch vs routerL1 repeats, L2 learns MACs, L3 routes IPVLANsVLANs802.1Q tag splits one switch into LANsARPARPbroadcast who-has IP, reply with the MACCRCCRCXOR division remainder catches bit errorsInternet ChecksumSum + NOT sum = 0xFFFF; blind to swapsWi-Fi CSMA/CAWi-Fi CSMA/CAlisten, random backoff, wait for the ACKALOHA and CSMA/CDALOHA and CSMA/CDFrame must last 2τ to hear a collisionCellular Reuse and HandoverCellular Reuse and HandoverMargin + timer: no ping-pong, no dropOpen the Networking mind mapNetworkingframes on one local networkLayers & linksProtocol layersProtocol layerseach layer wraps the one above in a headerHub vs switch vs routerHub vs switch vs routerL1 repeats, L2 learns MACs, L3 routes IPVLANsVLANs802.1Q tag splits one switch into LANsARPARPbroadcast who-has IP, reply with the MACCRCCRCXOR division remainder catches bit errorsInternet ChecksumSum + NOT sum = 0xFFFF; blind to swapsWi-Fi CSMA/CAWi-Fi CSMA/CAlisten, random backoff, wait for the ACKALOHA and CSMA/CDALOHA and CSMA/CDFrame must last 2τ to hear a collisionCellular Reuse and HandoverCellular Reuse and HandoverMargin + timer: no ping-pong, no drop
This page on the Networking mind map · 62 pages in 10 branches · open it to see the whole map and test yourself in recall mode.