gor.bio wiki

Big-Endian vs Little-Endian: Byte Order Explained

Big-endian and little-endian are the two ways computers order the bytes of a multi-byte number in memory or in a file, and mixing them up produces wrong values.

Category: Programming · Created: 2026-10-15 · Updated: 2026-10-15 · 3 min read

Illustration: 32-bit integer laid out in memory in big-endian and little-endian byte order
Illustration: 32-bit integer laid out in memory in big-endian and little-endian byte order · Image: Aeroid, CC BY-SA 4.0, via Wikimedia Commons.

Big-endian and little-endian are the two conventions for ordering the bytes of a multi-byte number in memory or in a file. A big-endian system stores the most significant byte first, at the lowest address. A little-endian system stores the least significant byte first. The names come from Jonathan Swift's Gulliver's Travels, in which two nations fight over which end of a boiled egg to crack open.

What is byte order?

A 32-bit integer takes four bytes. The hexadecimal value 0x12345678, which is 305,419,896 in decimal, is made of the bytes 12, 34, 56 and 78. In big-endian order they sit in memory as 12 34 56 78, starting with the largest part of the number. In little-endian order they sit as 78 56 34 12.

Think of memory as a row of numbered boxes, each holding one byte. A processor reads a multi-byte value by starting at one address and taking the next boxes in turn. The order of the bytes in those boxes decides which number it sees. The bytes never change, only their sequence does.

Why do two orders exist?

Neither order is more correct. Big-endian matches how people write numbers, with the largest digit on the left. Little-endian has some practical advantages inside a processor. Addition can start at the lowest byte and carry upward, and a smaller value can be read from the same starting address. The low 16 bits of a 32-bit little-endian integer, for example, begin at the same address as the whole number.

The terms were popularised in a 1980 paper by Danny Cohen, "On Holy Wars and a Plea for Peace," which used Swift's joke to describe the argument over which order computers should adopt. The labels stuck, but the argument did not end. Both orders remain in use.

Which systems use which order?

Most designs fix one order and stick to it. The table lists some common cases.

ContextByte order
Internet protocol headersBig-endian, called network byte order
x86 processors and most phone chipsLittle-endian
PNG image filesBig-endian
UTF-16 text filesEither, marked by a byte order mark

Some processors, such as ARM, can run in either order. In practice phones and laptops almost always run them in little-endian mode. The byte order mark, a special two-byte value at the start of a UTF-16 file, tells the reader which order the file uses.

Where does byte order cause bugs?

Byte order matters only when a value spans several bytes. A single byte has no order, and neither does a string of one-byte characters. The trouble starts when a program writes a number on one machine and reads it on another with the opposite order. If a little-endian machine reads the big-endian bytes 12 34 56 78 as one integer, it gets 0x78563412, which is 2,018,915,346 in decimal. Nothing crashes, so the wrong value can travel a long way before anyone notices.

Network code guards against this. Socket programs convert numbers with functions such as htonl and ntohl, which turn a value into network byte order before sending and back afterwards. File parsers check the format's specification or a marker such as the byte order mark. The same care applies to any binary format shared between systems.

Does this matter to ordinary programmers?

Most programmers never see byte order, because a language runtime or a library handles it. It becomes visible when code reads binary files, parses network packets, or exchanges memory with hardware. Memory safety is a separate question: a garbage collector or a language such as Rust controls who may touch a region of memory, but neither decides which byte comes first.

Developers who suspect a byte-order fault usually look at a hexadecimal dump of the file or packet, where a reversed value is easy to spot once the bytes are shown in order. Portable code states the order that its format uses and never assumes the order of the machine it runs on. Protocol designers face the same issue for every field that crosses a wire, as the headers described for TCP and UDP show.

Tags

data structures memory management networking programming

Related articles

More in Programming

All Programming articles

This text may be freely copied, modified, and reused. See Content Reuse.