Endianness

The idea of "endianness" refers to byte order.

In ordinary base-10 notation, we may have a number such as 751. The rightmost digit, 1, is called the least significant digit", and the leftmost digit, 7, is called the most significant digit.

In base 16, we have the same arrangement. If we have X'6B0A4CF8', the rightmost byte (two hexadecimal digits) is X'F8', the least significant byte, and the leftmost byte is X'6B', the most significant byte.

The question of endianness is how such a 4-byte base-16 number is stored in a computer's memory. It will be stored as 4 consecutive bytes, but in what order?

Big Endian: the most significant byte has the lowest address and the least significant byte has the highest address (1-2-3-4). For instance:

     Address    Contents

     0801         6B
     0802         0A
     0803         4C
     0804         F8

Little Endian: the least significant byte has the lowest address and the most significant byte has the highest address (4-3-2-1). For instance:

     Address    Contents

     0801         F8
     0802         4C
     0803         0A
     0804         6B

Either way, when this value is fetched from memory to the CPU across the memory bus, the bytes arrive in the above order, starting with the byte at address 0801.


Why would we prefer one of these to the other? If we have a 4-byte number already in the CPU (in a register) and we want to add it to a 4-byte number we are fetching from memory, then it is convenient to use the little-endian architecture, as we can start the Add operation as soon as the least signficant byte arrives. This speeds up the Add operation by some tiny amount. (Engineers care very much about such tiny improvements.)

On the other hand, if we have a 4-byte number already in the CPU (in a register) and we want to compare it to a 4-byte number we are fetching from memory, then it is convenient to use the big-endian architecture, as we can start comparing as soon as the most significant byte arrives. This speeds up the Compare operation by some tiny amount.

In general, most processors will use one of these two methods:

Big-Endian: IBM z/Architecture mainframes, Motorola 68000 series

Little-Endian: Intel processors

There are some processors that can operate in either fashion and are called "bi-endian".

There are also processors that use different methods for storing different kinds of data such as integers versus floating-point.

The term "mixed-endian" refers to processors (such as the PDP-11) that use other byte orders such as 3-4-1-2 or 2-1-4-3. That is, a 2-byte value may be stored in big-endian order but when two such values are stored together as one 4-byte value, they may be in little-endian order relative to each other.

There are probably similar questions about the storage of 8-byte values.

These terms are also sometimes used in referring to the storage of dates, as in YYYYMMDD (big-endian) or DDMMYYYY (little-endian) or MMDDYYYY (mixed-endian).

In sending bytes over wires in network protocols, the standard is to use "network order", which is big-endian. Not all protocols follow this standard.


The origin of the terms comes from a literary reference. In "Gulliver's Travels", Jonathan Swift described a situation in which two small nations fought wars over whether a soft-boiled egg should be opened at the large end or the small end.