Endianness in C++20

A portable way to check for system endianness in C++

CMP
2 min readSep 8, 2023

Endianness Basics

The endianness of a system refers to the order in which bytes are stored in memory or transmitted over a network. Big-Endian systems store the Most Significant Byte (MSB) at the lowest memory address. This is used in various network protocols to send data over a network, also known as Network Byte Order. In Little-Endian systems, the Least Significant Byte (LSB) is stored at the lowest memory address. For example, the hexadecimal number 0x12345678 would be stored as follows:

  • Big-Endian:
Address | Value
---------------
n | 0x12
n + 1 | 0x34
n + 2 | 0x56
n + 3 | 0x78
  • Little-Endian:
Address | Value
---------------
n | 0x78
n + 1 | 0x56
n + 2 | 0x34
n + 3 | 0x12

Depending on the architecture, your system may have a Host Byte Order of Big-Endian or Little-Endian. In x86 and x86–64 systems, Little-Endian is used. In ARM, both are supported but most devices will use Little-Endian. Some older architectures use Big-Endian.

Importance of Endianness

So, why is this important? If you need to send data over a network, then you need to ensure that it is transmitted in Network Byte Order. In most cases, your Host Byte Order will be Little-Endian, which means you would need to reverse the order of bytes before transmitting data. But what happens if you run the…

--

--

CMP

Software engineer specializing in operating systems, navigating the intracicies of the C++ language and systems programming.