Big-Endian vs Little-Endian
Byte order explained: how big-endian and little-endian lay out multi-byte numbers in memory, why network protocols pick one, and when the difference actually bites you.
A single byte has no ordering problem — it is just eight bits. But the moment you store a number that needs more than one byte, the machine has to decide which byte goes first. That decision is called endianness, and it quietly underpins everything from file formats to TCP packets.
What the two orders actually mean
Take the 32-bit hexadecimal value 0x12345678. It is four bytes: 12, 34, 56, and 78. The byte 12 is the most significant (it carries the largest place value), and 78 is the least significant. The only question is which one lands at the lowest memory address.
Big-endian stores the most significant byte first, at the lowest address. Reading memory from low to high, you see the bytes in the same order you would write them on paper:
address: +0 +1 +2 +3big-end: 12 34 56 78Little-endian stores the least significant byte first, at the lowest address. The bytes appear reversed relative to how you write the number:
address: +0 +1 +2 +3little: 78 56 34 12Both layouts represent the exact same value. Nothing is “backwards” in a moral sense — they are just two conventions, like driving on the left or the right. The names come from Jonathan Swift’s Gulliver’s Travels, where two factions go to war over which end of a boiled egg to crack first. Danny Cohen borrowed the metaphor in a 1980 paper to describe exactly this kind of arbitrary-but-consequential disagreement.
Where it matters in practice
For most code you will never type the word “endianness.” Within one running program, the CPU reads and writes its own native order consistently, so the layout is invisible. The trouble starts when bytes cross a boundary and a different reader has to interpret them.
The big split is between hardware and the network. The dominant desktop and server architectures — x86 and x86-64 — are little-endian, and most ARM chips run little-endian too (ARM can technically switch, but in practice almost everything you touch is little-endian). Internet protocols, however, standardized on big-endian decades ago. That convention is so entrenched it has a name: network byte order is big-endian. IP addresses, port numbers, and packet length fields all travel most-significant-byte-first.
So a little-endian machine sending a 16-bit port number over the wire must flip the bytes, and the receiver must flip them back. The C standard library gives you named helpers for this so you never have to think about which direction to swap:
uint32_t net = htonl(host_value); // host -> network (big-endian)uint32_t host = ntohl(received); // network -> host// htons/ntohs do the same for 16-bit valuesOn a big-endian host these functions are no-ops; on a little-endian host they perform the swap. Writing them either way keeps your code portable without special-casing the architecture.
The same applies to file formats. Some, like classic Windows BMP, are little-endian; others, like PNG and many network-oriented formats, are big-endian. A well-designed format simply documents its order and sticks to it, which is why robust serialization code reads and writes fields one byte at a time with explicit shifts rather than dumping a struct straight to disk.
How to stay safe
The reliable habit is to never assume the reader shares your byte order. When you control the format, pick an order, write it down, and convert explicitly at the edges. Most languages provide tools: Python’s struct module takes a format prefix (> for big-endian, < for little-endian), and many languages expose byte-swap intrinsics. The cost of a byte swap is negligible; the cost of a silent corruption that only shows up across architectures is not.
FAQ
FAQ
Which is faster, big-endian or little-endian?+
Is endianness about bit order too?+
How do I check my machine's endianness?+
Related reading
2026-06-04
ACID vs BASE: What Database Guarantees Actually Promise
ACID and BASE describe two ends of a tradeoff between strict correctness and scalable availability. Learn what each guarantee means, when each fits, and why most modern databases sit somewhere in between.
2026-06-04
Big-O Notation in Plain English
Big-O describes how an algorithm's runtime or memory grows as input grows. Learn the common classes — O(1), O(log n), O(n), O(n log n), O(n^2), O(2^n) — with plain examples.
2026-06-04
CORS in Plain English: Why the Browser Blocks Your Fetch
A clear walkthrough of CORS and the same-origin policy — what an origin is, why your fetch fails, how servers opt in, and the big misconception about who CORS actually protects.
2026-06-04
Environment Variables and PATH, Explained
What environment variables actually are, why they hold config and secrets, and how PATH decides which binary runs when you type a command.
2026-06-04
Floating Point: Why 0.1 + 0.2 Is Not 0.3
Type 0.1 + 0.2 into almost any language and you get 0.30000000000000004. Here is why IEEE 754 binary floating point does that — and how to handle it correctly.
Get the best tools, weekly
One email every Friday. No spam, unsubscribe anytime.