Endianness, also called byte order, is the convention that determines the sequence in which the bytes of a multi-byte value are stored in memory or transmitted over a channel. In software interoperability it matters whenever binary data leaves the program that created it, because two systems that disagree on byte order will read the same bytes as completely different numbers.
The question only arises for values wider than one byte, such as 16-bit, 32-bit and 64-bit integers, floating-point numbers and code units in UTF-16 or UTF-32 text. A single byte has no internal order to argue about, which is why plain ASCII and UTF-8 are immune to byte-order problems.
How byte order arranges a multi-byte value
Byte order describes which end of a number occupies the lowest memory address. A big-endian system stores the most significant byte first, so the bytes appear in the same order a person writes the hexadecimal digits. A little-endian system stores the least significant byte first, which reverses that visual order in a memory dump.
The table below shows how the 32-bit value 0x0A0B0C0D is laid out at four consecutive addresses under each convention.
| Byte order | Address 100 | Address 101 | Address 102 | Address 103 |
|---|---|---|---|---|
| Big-endian | 0A | 0B | 0C | 0D |
| Little-endian | 0D | 0C | 0B | 0A |
Neither order is faster or more correct in general. Little-endian layout lets a processor read the low-order part of a wider value at the same address, which simplified early arithmetic hardware. Big-endian layout keeps the numeric order readable in hex dumps and allows comparison of unsigned integers byte by byte from the start. The terms come from the egg-cracking factions in Gulliver’s Travels and were introduced to computing in 1980 to stress how arbitrary the choice is.
Byte order variants across hardware and formats
Most modern general-purpose processors are little-endian, while many network protocols and older file formats are big-endian. Some architectures are bi-endian, meaning the byte order can be selected at boot time or per process, although one mode usually dominates in practice.
| Platform or format | Byte order | Remarks |
|---|---|---|
| x86 and x86-64 | Little-endian | Dominant on desktops and servers |
| ARM (AArch64) | Bi-endian, little in practice | Mobile, Apple silicon, cloud servers |
| PowerPC and POWER | Bi-endian | Older systems big, recent Linux little |
| IBM z/Architecture | Big-endian | Mainframe workloads |
| TCP/IP headers | Big-endian | Called network byte order |
| Java class files and JVM data streams | Big-endian | Fixed by the specification |
| PNG, JPEG, MIDI | Big-endian | Fixed in the format definition |
| BMP, WAV, ZIP | Little-endian | Reflects their x86 origins |
| TIFF | Either | Marked by II or MM in the header |
Historical systems such as the PDP-11 also used middle-endian or mixed orders for 32-bit values, storing two 16-bit halves in an unusual sequence. These layouts are rare today, but they still appear when engineers migrate archived binary data from legacy machines.
Why endianness affects interoperability
Endianness becomes an interoperability problem the moment raw memory is written to a file, a socket or shared storage and read by a system with the opposite convention. A port number of 80, stored little-endian as 50 00, is read by a big-endian reader as 20480. Nothing crashes immediately, so the corruption often travels far before anyone notices it.
Well-designed formats avoid ambiguity in one of three ways, each with its own costs.
| Strategy | How it works | Trade-off |
|---|---|---|
| Fixed byte order | The specification mandates one order | Simple, but one side must always swap |
| Header marker | A flag or magic number declares the order | Writer stays native, reader must branch |
| Byte order mark | U+FEFF at the start of text signals order | Useful for UTF-16, harmful in some UTF-8 tools |
| Text representation | Numbers are written as characters | Order-neutral, but larger and slower |
| Self-describing serialization | A schema defines the encoding of every field | Portable, requires a library |
The byte order mark deserves particular attention in text interchange. In UTF-16 the sequence FE FF indicates big-endian and FF FE indicates little-endian; when a reader ignores it, the result is the scrambled text known as mojibake. Formats such as Protocol Buffers sidestep the issue by defining a canonical wire encoding independent of the host processor.
Handling endianness correctly in code
The reliable rule is to convert at the boundary: keep values in native order inside the program and translate them only when reading or writing an external representation. In C, the POSIX functions htonl, htons, ntohl and ntohs convert between host order and network order, and they compile to nothing on big-endian hosts.
- Decode by shifting, not by casting. Assembling a value with shifts and bitwise OR, such as
b[0] | b[1] << 8, produces the same result on every host. - Use explicit library APIs. Java
ByteBuffer.order(), .NETBinaryPrimitives, Pythonstructformat prefixes and Rustfrom_le_bytesstate the order in the code itself. - Document the order in every format specification. A binary layout without a declared byte order is an incomplete specification.
- Test on both orders. Emulators such as QEMU can run big-endian targets when no physical hardware is available.
Typical failures follow recognizable patterns, which the table below maps to their causes and remedies.
| Symptom | Likely cause | Fix |
|---|---|---|
| Huge or negative integers | Raw struct written and read on different hosts | Serialize each field with a defined order |
| Wrong port or IP address | Missing host-to-network conversion | Apply htons or htonl before sending |
| Garbled UTF-16 text | Ignored or missing byte order mark | Detect the BOM or declare UTF-16LE or UTF-16BE |
| Corrupted floating-point values | Bytes swapped for integers only | Swap the full IEEE 754 bit pattern |
| Double conversion | Swapping applied in two layers | Convert once, at a single documented boundary |
Limitations and common misconceptions
A frequent misconception is that endianness affects the order of bits within a byte. In byte-addressable memory, software never observes bit order directly; it matters only for serial transmission and bit-field layouts, which compilers define separately. Another error is assuming that character strings need swapping: an array of single bytes is stored identically on every host.
Endianness also does not solve other portability differences, such as structure padding, integer width and alignment rules. Writing a C struct directly to disk remains fragile even between two little-endian machines, because compilers may insert padding differently. Robust data portability therefore relies on explicit, field-by-field serialization rather than memory images.
A plain-language summary
Computers often store a large number by splitting it into several small pieces called bytes. Endianness is simply the rule for which piece goes first. Some machines put the biggest piece first, the way people write dates as year, month, day. Others put the smallest piece first, like writing day, month, year.
Both rules work perfectly inside one machine. Trouble starts only when data moves between machines that follow different rules, much like reading 03/04 as March 4 when the writer meant April 3. The fix is equally simple: agree on one order in advance and convert whenever data crosses that border.
An example from practice
A team maintaining a sensor-logging application exchanges binary measurement files between field devices built on a big-endian embedded processor and an analysis service running on x86-64 servers. Each record holds a 32-bit timestamp, a 16-bit sensor identifier and a 32-bit IEEE 754 temperature value.
After the first deployment, the dashboard shows sensor number 25600 instead of 100 and temperatures of absurd magnitude. An engineer inspects the file in a hex editor and sees the identifier stored as 00 64, which the server reads in little-endian order. The original reader had simply copied each record into a C struct.
The team rewrites the reader to decode every field explicitly as big-endian, adds a four-byte magic number and version field to the file header, and documents the layout in the format specification. Automated tests now replay sample files from both device generations, and the identifier and temperature errors disappear without any change to the field firmware.