logo

Endianness

Last Updated: 2021-12-07

Definition

  • Endianness: the order of the bytes

Big vs Little

  • big-endian: the most significant byte first
  • little-endian: the least significant byte first

Other names:

  • Network byte order: big-endian

Example:

0A 0B 0C 0D
  • Big-endian: stored as 0A 0B 0C 0D in memory
  • Little-endian: stored as 0D 0C 0B 0A in memory

Why Little-endian

A 32-bit memory location with content 4A 00 00 00 can be read at the same address as either 8-bit (value = 4A), 16-bit (004A), 24-bit (00004A), or 32-bit (0000004A), all of which retain the same numeric value.

Systems

  • big endian: Java, IPv6 (network byte order), IBM z/Architecture mainframes,
  • little endian: Intel x86 processor

In Action

In Python:

>>> import struct
>>> struct.pack("<I", 1)
b'\x01\x00\x00\x00'
>>> struct.pack(">I", 1)
b'\x00\x00\x00\x01'

where < means little-endian, and > means big-endian. I for 32-bit unsigned integer, so it takes 4 bytes. In little-endian, byte \x01 is stored first, while in big-endian, it is stored last.

Check system byte order:

>>> import sys
>>> sys.byteorder
'little'

Further Reading

https://docs.python.org/3/library/struct.html