Data representation · intermediate

Endianness

Endianness is the order in which the bytes of a multi-byte value are arranged in memory or a serialized representation. Big-endian places the most significant byte first; little-endian places the least significant byte first.

Why it matters

Programs must agree on byte order when reading binary files, network fields, device registers, foreign-memory interfaces, or byte buffers. A width-correct value can still be decoded incorrectly when order differs.

Mental model

How to reason about endianness

First split the value into whole bytes, then decide which byte occupies the lowest address or earliest serialized position. Endianness reorders bytes, not the bit significance within the abstract number.

Analogy

Endianness is an agreed order for placing the volumes of a numbered set on a shelf: the same volumes and numbering exist, but one convention shelves the most significant volume first and another the least significant.

Examples

See the boundary, not just the happy path

Worked example · Serialize big-endian

(0x12345678).to_bytes(4, byteorder='big').hex()  # 12345678

Python emits the most significant byte 0x12 first, followed by 0x34, 0x56, and 0x78. The width is explicitly four bytes.

Worked example · Serialize little-endian

(0x12345678).to_bytes(4, byteorder='little').hex()  # 78563412

The same numeric value produces the reverse byte sequence because the least significant byte 0x78 is emitted first.

Useful contrast · A single byte has no byte-order choice

0x7F occupies one byte

Endianness only distinguishes the ordering of multiple bytes. It does not reverse the bits inside this byte or change its value.

Common mistakes

Misconceptions to remove early

Reversing individual bits

Little-endian reverses the order of bytes in a multi-byte representation, not the bit order within each byte. Bit numbering is a separate convention.

Using host byte order as a file format

Persisting native-memory bytes without specifying order makes the format platform-dependent. Define an explicit byte order and convert at the boundary.

Quick check

Can you predict the result?

1. How is 0x1234 serialized as two little-endian bytes?
  • 34 12
  • 12 34
  • 21 43
Answer: 34 12
2. Why does endianness not matter for a one-byte field?
Answer: There is only one byte, so there is no relative byte order to choose.

Keep building

Authoritative references

Make the idea retrievable.

Study this concept with spaced repetition, next to the commands where you use it.

Get Terminaster