Endianness defines the order in which bytes that represent large numerical values are stored in memory.
There are two types of endianness:
- Big-endian: The most significant byte is stored first. A 32-bit value, 0x01020304, is stored at the ptr address, as follows:
Offset in memory (byte) Value ptr 0x01 ptr + 1 0x02 ptr + 2 ox03 ptr + 3 0x04
Examples of big-endian architectures include AVR32 and Motorola 68000.
- Little-endian: The least significant byte is stored first. A 32-bit value, 0x01020304, is stored at the ptr address, as follows:
Offset in memory (byte) Value ptr 0x04 ptr + 1 0x03 ptr + 2 0x02 ptr + 3 0x01
The x86 architecture is little-endian.
Taking care of endianness is especially essential when exchanging data with other systems. If a developer sends a 32-bit integer, say, 0x01020304, as it is, it may be read as 0x04030201 if the endianness of the receiver does not match the endianness of the sender. That is why data should be serialized.
In this recipe, we will learn how to determine the endianness of our target system.