endianness

term

programmingnetworking

Kept in this browser · My desk

Stands for: byte order: big-endian (most significant byte first) or little-endian (least significant byte first)

The order in which a processor, a file format or a protocol lays out the bytes of a number wider than one byte: big-endian puts the most significant byte at the lowest address, little-endian the least significant.

Danny Cohen named the two orders in 1980, after the egg war in Gulliver's Travels. Internet headers are big-endian, which RFC 791 states and POSIX calls network byte order; x86-64 and most Arm systems are little-endian; IBM z is big-endian; Power, RISC-V, MIPS and SPARC can run either way, which is called bi-endian. Code converts at every boundary with htonl, ntohs and their relatives, and a missed conversion produces a valid wrong number rather than an error: port 80 written in host order on a little-endian machine is read from the wire as port 20480.

What it is. The 32-bit value 0x0A0B0C0D is stored as the bytes 0A 0B 0C 0D on a big-endian machine and 0D 0C 0B 0A on a little-endian one. POSIX defines the two: a little-endian representation has the least significant byte first, with significance increasing as the address increases, and a big-endian one the most significant byte first. Only numbers wider than a byte are affected: single bytes, ASCII text and UTF-8 look the same either way, and the order of bits inside a byte is the link layer's business, not software's (RFC 2469 records that Ethernet maps the first bit of each byte on the wire to its least significant bit, while Token Ring did the opposite).

Where it is fixed. Intel's manual calls Intel 64 and IA-32 processors little endian machines, and IBM's z/Architecture addresses a field by its leftmost, high-order byte. The Power ISA lets the operating system choose the order with one mode bit; RISC-V defines little-endian, big-endian and bi-endian variants but always stores instructions as little-endian parcels; SPARC V9 is big-endian by default with little-endian data accesses; Linux describes MIPS systems as built for one order or the other. Among formats, the Internet headers, PNG, Java class files, XDR (External Data Representation) and CBOR are big-endian; BMP and WebAssembly memory are little-endian; TIFF marks its order with II or MM, ELF with the EI_DATA byte, and UTF-16 text with a byte order mark, without which the Unicode Standard and RFC 2781 read it as big-endian while browsers, following the WHATWG Encoding Standard, take the label utf-16 to mean UTF-16LE.

Converting. The conversions are cheap and named by direction: htonl and ntohl in POSIX, htobe32 and le32toh in its newer endian.h, cpu_to_be32 in the Linux kernel, the ! and the less-than and greater-than prefixes of Python's struct. Processors carry instructions for it: BSWAP and MOVBE on x86, LOAD REVERSED on IBM z, the byte-reverse loads and stores on Power, rev8 in RISC-V's Zbb extension. To find out what a machine is, Python's sys.byteorder answers little or big, and lscpu prints a Byte Order line.

Why it matters. Neither order wins on merit: Cohen's memo lists what serial comparators prefer and what serial adders prefer, and concludes that it is more important to agree upon an order than which order is agreed upon. RISC-V's designers chose little-endian because it dominates commercially, and kept big-endian variants because IP networking works on big-endian data; RFC 4506 admits that XDR's single big-endian order is unfair to little-endian machines, and argues that one order means no protocol is needed to read the data. The real cost is the conversion at each boundary and the bugs when one is missed: a port or an address loaded raw, UTF-16 decoded in the wrong order, a hash taken over raw bytes. The Linux bonding driver hashes raw address bytes, so an x86-64 server and an s390x guest compute different hashes for the same flow, and with three, five, six or seven members they often send it over different links.

Also known as: byte order, big-endian, little-endian, bi-endian, network byte order, host byte order, byte swap, byteswap, ordem de bytes, ordem dos bytes, ordem de rede

Sources

← Back to E Back to the glossary