Endianness

Also known as: byte order

On this page 7

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 orderAddress 100Address 101Address 102Address 103
Big-endian0A0B0C0D
Little-endian0D0C0B0A
Storage of the value 0x0A0B0C0D at addresses 100 to 103

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 formatByte orderRemarks
x86 and x86-64Little-endianDominant on desktops and servers
ARM (AArch64)Bi-endian, little in practiceMobile, Apple silicon, cloud servers
PowerPC and POWERBi-endianOlder systems big, recent Linux little
IBM z/ArchitectureBig-endianMainframe workloads
TCP/IP headersBig-endianCalled network byte order
Java class files and JVM data streamsBig-endianFixed by the specification
PNG, JPEG, MIDIBig-endianFixed in the format definition
BMP, WAV, ZIPLittle-endianReflects their x86 origins
TIFFEitherMarked by II or MM in the header
Typical byte order of common platforms and formats

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.

StrategyHow it worksTrade-off
Fixed byte orderThe specification mandates one orderSimple, but one side must always swap
Header markerA flag or magic number declares the orderWriter stays native, reader must branch
Byte order markU+FEFF at the start of text signals orderUseful for UTF-16, harmful in some UTF-8 tools
Text representationNumbers are written as charactersOrder-neutral, but larger and slower
Self-describing serializationA schema defines the encoding of every fieldPortable, requires a library
Strategies that formats use to handle byte order

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(), .NET BinaryPrimitives, Python struct format prefixes and Rust from_le_bytes state 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.

SymptomLikely causeFix
Huge or negative integersRaw struct written and read on different hostsSerialize each field with a defined order
Wrong port or IP addressMissing host-to-network conversionApply htons or htonl before sending
Garbled UTF-16 textIgnored or missing byte order markDetect the BOM or declare UTF-16LE or UTF-16BE
Corrupted floating-point valuesBytes swapped for integers onlySwap the full IEEE 754 bit pattern
Double conversionSwapping applied in two layersConvert once, at a single documented boundary
Common endianness defects and how to fix them

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.