Endianness is a common embedded interview topic covering the definition of byte order, how to inspect host byte order in C, and what to watch out for when exchanging binary data across networks or files. The test environment used in this post is Linux on a little-endian host.

Key takeaways

Endianness is the order in which the bytes of a multi-byte value are placed in memory. Little-endian stores the least significant byte (LSB) at the lowest memory address, while big-endian stores the most significant byte (MSB) at the lowest address. Reading 0x12345678 byte by byte on a little-endian host yields 78 56 34 12, and htonl converts it to network byte order (big-endian) before sending over a network.

What is endianness?

One-line answer: Endianness is the order in which the bytes of a multi-byte value are arranged in memory.

Endianness refers to the order of bytes of a multi-byte value in memory, not the order of bits inside an individual byte.

When building software for another architecture, target properties such as byte order must be kept in mind; see AArch64 Cross-Compile with CMake: How to Use a Toolchain File.

How do big-endian and little-endian differ?

One-line answer: Big-endian places the most significant byte (MSB) at the lowest memory address, whereas little-endian places the least significant byte (LSB) at the lowest memory address.

In 0x12345678, the most significant byte (MSB) is 0x12 and the least significant byte (LSB) is 0x78. Across memory addresses 0x00 through 0x03, little-endian stores the bytes as 78 56 34 12, while conceptually big-endian stores them as 12 34 56 78.

Byte OrderAddress 0x00Address 0x01Address 0x02Address 0x03
Little-endian78 (LSB)563412 (MSB)
Big-endian (concept)12 (MSB)345678 (LSB)

Little-endian vs big-endian byte layout for 0x12345678

How do you check the endianness of your system?

One-line answer: Read the bytes of a multi-byte integer through an unsigned char * pointer in C, or run lscpu in a Linux shell.

Reading a value through an unsigned char * pointer to inspect its bytes is the usual approach in C. Below is endian.c, compiled with gcc (Debian 14.2.0-19) 14.2.0 using gcc -std=c11 -Wall endian.c -o endian (no warnings) and run via ./endian. In the code comments, the first comment means “read byte by byte” and the second means “network byte order (big-endian)”.

#include <stdio.h>
#include <stdint.h>
#include <arpa/inet.h>
int main(void) {
    uint32_t v = 0x12345678;
    const unsigned char *p = (const unsigned char *)&v;   /* 바이트 단위로 읽기 */
    printf("memory: %02x %02x %02x %02x\n", p[0], p[1], p[2], p[3]);
    printf("%s-endian\n", p[0] == 0x78 ? "little" : "big");
    uint32_t n = htonl(v);                                 /* 네트워크 바이트 순서(빅엔디안) */
    const unsigned char *q = (const unsigned char *)&n;
    printf("htonl:  %02x %02x %02x %02x\n", q[0], q[1], q[2], q[3]);
    printf("ntohl(htonl(v)) == v: %s\n", ntohl(n) == v ? "yes" : "no");
    return 0;
}

The program output is:

memory: 78 56 34 12
little-endian
htonl:  12 34 56 78
ntohl(htonl(v)) == v: yes

Running lscpu | grep -i "byte order" on the same host prints:

Byte Order:                              Little Endian

In gdb, you can also inspect memory bytes with the x command; see How to Find Crash Causes with gdb in Embedded Linux: Core Dump and gdbserver in Practice.

Why does endianness matter in communication and files?

One-line answer: If devices with different byte orders exchange multi-byte binary data without conversion, the byte sequence is reversed and interpreted as a different value.

According to byteorder(3) (POSIX.1-2008), network byte order as used on the Internet is Most Significant Byte first (big-endian), whereas on a little-endian host the host byte order is Least Significant Byte first. Declared in <arpa/inet.h>, htonl and htons convert values from host byte order to network byte order, and ntohl and ntohs convert them back. As shown in the output lines htonl: 12 34 56 78 and ntohl(htonl(v)) == v: yes above, when exchanging binary data or files between devices with different byte orders, both sides must agree on a byte order and convert accordingly.

Host byte order vs network byte order (big-endian) conversion

For a comparison of communication interfaces, see I2C vs SPI vs UART: How to Compare Them in an Embedded Interview.

What endianness questions come up in interviews?

One-line answer: Common questions cover char arrays (strings), sending raw structs with padding, bit-field layout, and how C and C++ differ on checking endianness with a union.

Does endianness affect char arrays (strings)?

Each element of a char array or string is a single byte, so byte order does not change the order of the characters; endianness matters for multi-byte values.

Why shouldn't you send a raw struct directly?

Compilers may insert padding between members for alignment, so the raw bytes of a struct can differ between compilers and architectures in addition to byte order.

What should you watch out for with bit-fields?

How bit-fields are laid out inside their storage unit (including order) is implementation-defined, so do not send bit-field structs as raw bytes between different systems.

Can you check endianness using a union?

In C (since C99 TC3), reading a union member other than the one last written reinterprets the stored object representation (type punning). In C++, reading from a union member that was not most recently written is undefined behavior, though many compilers allow it as a non-standard extension; in C++, prefer reading bytes through an unsigned char * view.

One-line answer: These related posts cover cross-compiling with CMake, the embedded Linux boot flow, debugging memory with gdb, and communication interfaces.

What are the sources?

One-line answer: This post is based on the Linux byteorder(3) manual page and the C and C++ union documentation on cppreference.