Understanding endianness: little-endian and big-endian

Introduction

During the execution of an application, processors work with numerical values called variables. For example, a processor may add, subtract or multiply variables. A variable is stored at a given address in the memory (or rather from a given address). It also has a certain size, which can be expressed either in bytes or in bits: it may be a single-byte (8 bits) or a multi-byte (n x 8 bits) value.

When the variable is a single-byte value, the address of the variable obviously points to its unique byte.

But, in the case of a multi-byte value, which of its bytes do its address point to, and in which order are they stored? The answer is: it depends.

Definition of endianness

In computer science, the term “endianness” refers to the order in which bytes are stored in memory. The endianness depends on the architecture of a processor.

The concept of endianness also exists for network protocols for the order of bytes in a frame (message), and for files formats for the order of bytes in a data stream.

The two main types of endianness are little-endian and big-endian.

Here is a comparison of these two endianness types:

Endianness type Little-endian (LE) Big-endian (BE)
Bytes order The least significant byte (LSB) (the “little end”) is stored first (at the lowest memory address). The most significant byte (MSB) (the “big end”) is stored first (at the lowest memory address).
Processor architectures examples Intel x86
Most ARM-based processors: Apple silicon, STM32, PIC32
ESP32
RISC-V
MSP430
Motorola 68000
PowerPC
SPARC
Network protocols examples USB
CANopen
TCP/IP
Modbus
Files formats examples BMP
GIF
PNG
JPEG
Other names LSB-first
Intel order
MSB-first
Motorola order

Examples

Let’s see the impact of endianness for a single-byte variable, with the value 0x42:

Endianness comparison for a 8-bits variable

Obviously, in the case of a single-byte value, the endianness has no impact, as the pointer to the value points to the unique byte, regardless of endianness.

Let’s now see the impact of endianness for a 32-bits (4 bytes) multi-byte variable, with the value 0x12345678:

Endianness comparison for a 32-bits variable

For a multi-byte value, the endianness has a notable influence. The pointer (lowest memory address among all bytes of the variable) points to the least significant byte (LSB) in the case of a little-endian system, while it points to the most significant byte (MSB) in the case of a big-endian system.

When might endianness be an issue ?

In many cases, we don’t need to worry about endianness. In an application in which we simply read and write multi-byte variables and use them, the processor reads and stores them according to its “natural” endianness.

However, in some specific situations, proper handling of endianness is crucial:

  • Network protocols

    When sending or receiving data of a network protocol, data are generally stored in a memory buffer in the same order as the bytes are exchanged. If the endianness of the protocol is different from the endianness of the machine, bytes swapping is required for multi-byte values.

  • Shared memory

    When values are stored in a shared memory and shared by two (or more) processes running on different processors based on different architectures, the endianness may be different. If this is the case, a common format (endianness) shall be defined, and bytes of multi-byte values shall be swapped whenever required.

  • Binary files

    When working with binary files, the endianness of the file format shall be known, in order to properly interpret or encode the data of the file. The endianness for a given file format may be fixed (i.e. defined in the format specification), or dynamically indicated within the file.

  • Pointers arithmetic for bytes manipulation

    When knowing the endianness of an application, it is possible to get a single byte of a variable by manipulating a pointer to it, by adding an offset to its base address. Endianness of the system shall then be considered, in order to know the bytes order.

Conclusion

In computer science, endianness is a simple, but crucial concept to understand, especially in low-level programming, networking and manipulation of binary files.

It can be seen as a convention for encoding of multi-byte values: the endianness defines if the least significant byte (little-endian) or the most significant byte (big-endian) is stored first, i.e. at the lowest memory address.