Operating systems / Endianness

Endianness

A number too big for one byte is stored as several, and computers disagree about which comes first. One 32-bit value, followed byte by byte through memory, a bug and a network.

Beginner7 steps~6 min
Big end — most significant byte: 12
Little end — least significant byte: 78
Memory cell — holds one byte at one address
// THE SHORT VERSION

In 30 seconds

A large number takes several bytes. Endianness is the order in which a computer lays those bytes out in memory.

Big-endian puts the most significant byte first, the way we write numbers. Little-endian puts the least significant byte first. Most PCs and phones are little-endian.

Inside one machine the choice is invisible. It matters when bytes are saved to a file or sent to another computer: read in the wrong order, they give a different number. Internet protocols avoid this by agreeing on big-endian.

Endianness, step by step

STEPS01 / 07

Endianness reorders whole bytes, never the bits inside one, and single-byte data such as ASCII text is unaffected. Many ARM chips can run in either order; little-endian is what is used in practice.

STEP 01 / 07 One number, four bytes
THE MATH
0x12345678 = 12 34 56 78 (4 bytes) = 305 419 896
  1. Step 1: One number, four bytes. A byte holds a number from 0 to 255, so a larger value is spread over several bytes. Written in hexadecimal, each pair of digits is exactly one byte: 0x12345678 splits into 12, 34, 56 and 78. The 12 carries the most weight and is called the big end; the 78 carries the least, the little end.
  2. Step 2: Four bytes, four addresses. Memory is a long row of cells, each holding one byte and each with its own address. Our value needs four cells in a row, here 0x100 to 0x103. Nothing in the number says which byte belongs in the first cell. That decision is the byte order, or endianness.
  3. Step 3: Big-endian: big end first. A big-endian machine stores the most significant byte at the lowest address, so memory reads in the same order as the number is written: 12 34 56 78. IBM mainframes and SPARC processors work this way, and so do most network protocols.
  4. Step 4: Little-endian: the reverse. A little-endian machine does the opposite: the least significant byte goes to the lowest address, so the same value sits in memory as 78 56 34 12. Only the order of whole bytes changes; the digits inside each byte stay as they are. x86 processors and nearly all ARM phones and laptops are little-endian.
  5. Step 5: Reading it back, byte by byte. To rebuild the value, a little-endian machine gives the byte at the lowest address a weight of 1 and multiplies the weight by 256 for each address after it. Hex 78 is 120, 56 is 86, 34 is 52 and 12 is 18. The four products add up to 305 419 896, which is 0x12345678 again. Nothing was lost.
  6. Step 6: The bug: a wrong assumption. Trouble starts when bytes leave the machine, in a file or over a network. A little-endian program writes 78 56 34 12; a reader that expects big-endian takes 78 as the big end and gets 0x78563412, which is 2 018 915 346. Nothing crashes and no error appears. The number is simply wrong.
  7. Step 7: Network byte order. The cure is to agree on one order for data in transit. Internet protocols use big-endian, known as network byte order. A program converts with htonl() before sending and ntohl() after receiving, so each host only needs to know its own order. File formats settle it the same way: PNG is big-endian, BMP little-endian.
VALUES SO FAR
value0x12345678input
decimal305 419 896input
cells····address
big····memory
little····memory
misread····bug
that is····bug
wire····network

Spotted a mistake on this page?