5.1.11Instruction Set Architecture (ISA)

Endianness (big vs little)

1,794 words8 min readdifficulty · medium

WHAT is endianness?

The word comes from Gulliver's Travels (which end of a boiled egg to crack). It's a convention, not a correctness question — both work fine, they just must agree.


WHY does it even exist?


HOW to lay bytes in memory (derive it)

Take the 32-bit hex value: V=0x12345678V = \texttt{0x12345678}

Split into bytes (each 2 hex digits = 8 bits):

Byte Value Significance
MSB 0x12 highest (×224\times 2^{24})
0x34 ×216\times 2^{16}
0x56 ×28\times 2^{8}
LSB 0x78 lowest (×20\times 2^{0})

Reconstruction formula (this is why significance matters): V=0x12224+0x34216+0x5628+0x7820V = \texttt{0x12}\cdot 2^{24} + \texttt{0x34}\cdot 2^{16} + \texttt{0x56}\cdot 2^{8} + \texttt{0x78}\cdot 2^{0}

Now store starting at address AA:

Big-endian (MSB at lowest address):

A+0: 12   A+1: 34   A+2: 56   A+3: 78

Little-endian (LSB at lowest address):

A+0: 78   A+1: 56   A+2: 34   A+3: 12
Figure — Endianness (big vs little)

Worked examples


Common mistakes


Detecting endianness (classic trick)


Flashcards

What does endianness determine?
The order in which the bytes of a multi-byte value are stored across consecutive memory addresses.
In big-endian, which byte is at the lowest address?
The most significant byte (MSB).
In little-endian, which byte is at the lowest address?
The least significant byte (LSB).
Store 0x12345678 little-endian starting at addr A.
A+0:78, A+1:56, A+2:34, A+3:12.
Store 0x12345678 big-endian starting at addr A.
A+0:12, A+1:34, A+2:56, A+3:78.
Does endianness reverse bits within a byte?
No — only the ordering of whole bytes; bits inside a byte are unchanged.
What is "network byte order"?
Big-endian, the standard used for transmitting multi-byte fields in TCP/IP.
Which endianness do x86 CPUs use?
Little-endian.
Formula for the k-th byte (k=0 is LSB) of value V.
b_k = floor(V / 256^k) mod 256.
Byte stored at offset i, big-endian, N-byte value?
mem[i] = b_{N-1-i}.
Quick runtime test: store int 1, read byte at lowest address; result 1 means?
Little-endian.
Is a char[] string affected by endianness?
No — array elements keep their order; only multi-byte scalars are split by endianness.

Recall Feynman: explain to a 12-year-old

Imagine a big number that's too long to fit in one mailbox, so you split it into chunks and put each chunk in a row of mailboxes. Big-endian puts the biggest chunk in the first mailbox (like writing a number normally, left to right). Little-endian puts the smallest chunk first. The number you get back is the same either way — you just have to know which house's rule you're using, otherwise you'd read the number scrambled. That's it: same number, different chunk-order in the mailboxes.


Connections

  • Instruction Set Architecture (ISA) — endianness is an ISA design choice the CPU commits to.
  • Memory Addressing — depends on byte-addressable memory and address ordering.
  • Data Representation — builds on how integers map to bytes (V=bk256kV=\sum b_k 256^k).
  • Network Protocols — "network byte order" is big-endian; requires htonl/ntohl conversions.
  • Pointers and Type Punning — the union trick used to detect endianness.
  • Bitwise Operations — byte extraction uses shifts/masks, not endianness itself.

Concept Map

defines order of

laid across

is a

splits into

splits into

puts

puts

derived from

derived from

benefit

benefit

Endianness

Multi-byte value

Byte-addressable memory

Storage convention

Big-endian

Little-endian

MSB at lowest address

LSB at lowest address

Byte formula bk = floor V/256^k mod 256

Matches human digit order

Simpler carry hardware

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Endianness ka matlab bas itna hai: jab koi number ek byte se bada hota hai (jaise 4-byte ka integer), toh uske bytes ko memory ke consecutive addresses mein kis order mein rakha jaaye. Number wahi rehta hai, sirf storage order badalta hai — bilkul date likhne jaisa, koi DD/MM likhta hai koi MM/DD, din toh same hi hai.

Do conventions hain. Big-endian mein sabse bada (most significant) byte lowest address pe jaata hai — "big end first", jaise hum number left-to-right likhte hain. Little-endian mein sabse chhota (least significant) byte lowest address pe jaata hai. Yaad rakhne ka trick: "Big end → Beginning". Example: 0x12345678 ko address 100 se store karo — little-endian mein 100:78, 101:56, 102:34, 103:12, aur big-endian mein 100:12, 101:34, 102:56, 103:78.

Ye important kyun hai? Kyunki x86 aur zyadatar ARM chips little-endian hote hain, lekin internet/network par data big-endian ("network byte order") mein bheja jaata hai. Agar do machines alag convention use karti hain aur convert nahi karti, toh number ulta-pulta padha jaayega — 0x12345678 ban jaayega 0x78563412. Isliye programmers htonl/ntohl jaise functions use karte hain.

Ek common galti: log samajhte hain ki endianness bits ko reverse karti hai — nahi! Sirf poore bytes ka order badalta hai, ek byte ke andar ke bits kabhi nahi badalte. Aur single byte ya char[] string par endianness ka koi asar nahi — sirf multi-byte scalar (int, float) par lagti hai. Bas yahi core idea hai, ise clearly samajh lo toh sab clear.

Go deeper — visual, from zero

Test yourself — Instruction Set Architecture (ISA)

Connections