networking · advanced · ~35 min
One's complement arithmetic + carry-fold.
The 16-bit one's-complement "Internet checksum" is the integrity check used by IP, TCP, and UDP. Compute it over an arbitrary byte buffer.
Implement uint16_t inet_checksum(const uint8_t *buf, size_t len) following these steps:
len is odd, treat the final byte as the high byte of a zero-padded word.sum = (sum & 0xFFFF) + (sum >> 16).~sum) as a 16-bit value.buf, len: the bytes to checksum and their count. len may be 0 or odd.Returns the 16-bit checksum.
{0x00,0x01, 0x00,0x02} -> 0xFFFC (words 0x0001 + 0x0002 = 0x0003; ~0x0003)
{0x12,0x34,0xAB} -> 0x42CB (odd length: words 0x1234 + 0xAB00)
{0xFF,0xFF, 0xFF,0xFF} -> 0x0000 (carry-fold: 0x1FFFE -> 0xFFFF -> ~ = 0)
len == 0): the checksum is 0xFFFF (~0).The 1s-complement checksum is the TCP/UDP/IP integrity check. Writing it from scratch demystifies the byte-level mechanics of every packet that crosses the internet.
A byte buffer (buf) and its length (len); len may be 0 or odd.
The 16-bit one's-complement Internet checksum.
Treat bytes as unsigned; word order is big-endian (high byte first); fold carries before returning ~sum.
#include <stdint.h>
#include <stddef.h>
uint16_t inet_checksum(const uint8_t *buf, size_t len) { /* TODO */ return 0; }
Treating bytes as signed (sign extension wrecks the sum); forgetting to fold carries; forgetting that a 0xFFFF result is encoded as 0x0000 by sender convention (you do not need to apply that here; just return ~sum).
Empty buffer → checksum is 0xFFFF (~0). Single byte → padded to a word.
O(len).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.