Before we start, one reminder of the single formula the whole page rests on. For an N-byte value V, number its bytes with k=0 as the least significant byte (LSB). Then
bk=⌊256kV⌋mod256
256k=28kshifts the low k bytes out of the way (each byte is 8 bits).
mod256masks off everything except the lowest remaining byte.
And placement into memory at base address A, offset i:
Little-endian: mem[A+i]=bi (LSB goes to lowest address)
Big-endian: mem[A+i]=bN−1−i (MSB goes to lowest address)
The reference figure below is the mental picture for every problem: four mailboxes at addresses A+0…A+3, and the two rules for filling them.
WHAT we check: does the byte at the lowest address match the most significant byte or the least significant byte of the value?
The value's MSB is 0xAA (the leftmost pair, weight 224). The lowest address holds AA.
MSB at lowest address ⇒ big-endian ("Big end → Beginning").
Recall Solution 1.2
Lowest address holds DD, which is the LSB (weight 20). LSB at lowest address ⇒ little-endian.
Recall Solution 1.3
Big-endian. See Network Protocols — that is why htonl/ntohl exist: they convert between the host's order and this agreed big-endian wire order.
Extract bytes (k=0 is LSB): b0=EF,b1=BE,b2=AD,b3=DE.
Why:bk=⌊V/256k⌋mod256; e.g. b1=⌊V/256⌋mod256=BE.
Place little-endian (mem[200+i]=bi):
200:EF 201:BE 202:AD 203:DE.
Recall Solution 2.2
Big-endian reverses the index: mem[200+i]=b3−i.
200:DE 201:AD 202:BE 203:EF.
Check: biggest byte (DE) landed at the smallest address — matches "Big end first."
Recall Solution 2.3
Bytes: b0=FF (LSB), b1=00 (MSB).
Little-endian (mem[10+i]=bi): 10:FF 11:00.
Big-endian (mem[10+i]=b1−i): 10:00 11:FF.
Why N matters: the big-endian index is N−1−i, so the reversal length depends on the value's width.
In little-endian the byte at offset iis byte index k=i, so weight it by 256i:
V=78⋅2560+56⋅2561+34⋅2562+12⋅2563=0x12345678.
Recall Solution 3.2
In big-endian mem[i]=bN−1−i, so the byte at offset 0 is the MSB. Weight the lowest address most:
V=78⋅2563+56⋅2562+34⋅2561+12⋅2560=0x78563412.Lesson: identical bytes, opposite convention ⇒ a completely different number. This is exactly why two machines must agree.
Recall Solution 3.3
Writer (little-endian) lays: 0:44 1:33 2:22 3:11.
Reader (big-endian) treats offset 0 as MSB:
V=44⋅2563+33⋅2562+22⋅2561+11⋅2560=0x44332211.
The value got byte-reversed — the classic bug fixed by ntohl in Network Protocols.
Little-endian bytes: 0:09 1:00 2:00 3:00. One byte at address 0 = 0x09 = 9 — correct low-order value, widening is "free."
Big-endian bytes: 0:00 1:00 2:00 3:09. One byte at address 0 = 0x00 = 0 — wrong; the 9 lives at address 3.
Why: little-endian keeps low-order data at low addresses, so narrow reads work directly (see Memory Addressing).
Recall Solution 4.2
x=1 has bytes b0=01, b1=b2=b3=00.
Little-endian: lowest address holds b0=01 ⇒ b == 1.
Big-endian: lowest address holds the MSB b3=00 ⇒ b == 0.
The location of the single nonzero byte reveals the convention.
Recall Solution 4.3
V=(0x12≪24)∣(0x34≪16)∣(0x56≪8)∣0x78.
Equivalently V=0x12⋅224+0x34⋅216+0x56⋅28+0x78.
This is independent of endianness: shifts operate on the value (the abstract number), never on memory addresses. Endianness only appears when that value is laid out in bytes. See Data Representation.
char[] is an array of single bytes, not one multi-byte scalar, so endianness does not touch it.
Both machines: 0:48 ('H') 1:69 ('i') 2:00 (NUL).
Endianness reorders bytes inside one wide integer; array element order is fixed by the array itself.
Recall Solution 5.2
Intended (big-endian reader): offset 0 is MSB ⇒ V=01⋅256+00=0x0100=256.
Little-endian host, no conversion: treats offset 0 as LSB ⇒ V=01+00⋅256=0x0001=1.
It reads 1 instead of 256 — off by a byte-swap. Correct handling: ntohs swaps back. This is the everyday reason network code converts explicitly.
Recall Solution 5.3
Store: mem[i]=bi=⌊V/256i⌋mod256.
Read back by weighting each offset by 256i:
V′=∑i=0N−1mem[i]⋅256i=∑i=0N−1bi⋅256i.
But ∑ibi⋅256i is precisely the base-256 expansion of V, so V′=V. The convention is lossless as long as write rule = read rule. (The same holds big-endian with 256N−1−i.)