Endianness: Big-Endian vs Little-Endian Byte Order
Understand big-endian and little-endian byte ordering. Learn how multi-byte values are stored in memory and why byte order matters for cross-platform code.
Detailed Explanation
Endianness describes the order in which bytes of a multi-byte value are stored in memory. This seemingly simple concept is a major source of bugs in cross-platform software, network programming, and binary file parsing.
Big-endian (network byte order):
The most significant byte is stored at the lowest memory address. The number 0x12345678 is stored as:
Address: 0x00 0x01 0x02 0x03
Value: 0x12 0x34 0x56 0x78
This matches how humans write numbers (most significant digit first). TCP/IP network protocols use big-endian, which is why it is also called "network byte order."
Little-endian (Intel byte order):
The least significant byte is stored at the lowest memory address. The same 0x12345678 is stored as:
Address: 0x00 0x01 0x02 0x03
Value: 0x78 0x56 0x34 0x12
Most modern CPUs (x86, x86-64, most ARM configurations) use little-endian.
Why little-endian exists:
Little-endian simplifies certain hardware operations. When you cast a 32-bit integer to a 16-bit integer, the value is already at the same memory address — no pointer adjustment needed. Addition also proceeds naturally from the lowest byte upward.
When endianness causes problems:
- Network communication: Data sent from an Intel machine (little-endian) to a SPARC machine (big-endian) will be garbled unless byte-swapped. Functions like
htonl()andntohl()handle this. - Binary file formats: File formats specify their endianness. BMP files use little-endian. TIFF files include an endianness marker. Reading a file with the wrong byte order produces nonsensical values.
- Hexdump analysis: When reading a hexdump of memory from an x86 system, a 32-bit integer
0x00000001appears as01 00 00 00, not00 00 00 01.
Detecting endianness in code:
In JavaScript: new Uint8Array(new Uint32Array([1]).buffer)[0] === 1 returns true on little-endian systems. In Python: import sys; sys.byteorder returns 'little' or 'big'.
Use Case
Embedded systems engineers handle endianness conversions when writing drivers that communicate between a little-endian ARM processor and big-endian network protocols or peripherals.
Try It — Number Base Converter
Related Topics
How to Read a Hexdump
Raw Binary Data → Hexadecimal + ASCII
Convert Binary to Hexadecimal
Binary (Base 2) → Hexadecimal (Base 16)
Convert Hexadecimal to Binary
Hexadecimal (Base 16) → Binary (Base 2)
Convert Binary to Decimal
Binary (Base 2) → Decimal (Base 10)
IEEE 754 Floating Point Binary Representation
Decimal (Floating Point) → Binary (IEEE 754)