5.1.11 · D4Instruction Set Architecture (ISA)

Exercises — Endianness (big vs little)

1,781 words8 min readBack to topic

Before we start, one reminder of the single formula the whole page rests on. For an -byte value , number its bytes with as the least significant byte (LSB). Then

  • shifts the low bytes out of the way (each byte is 8 bits).
  • masks off everything except the lowest remaining byte.

And placement into memory at base address , offset :

  • Little-endian: (LSB goes to lowest address)
  • Big-endian: (MSB goes to lowest address)

The reference figure below is the mental picture for every problem: four mailboxes at addresses , and the two rules for filling them.

Figure — Endianness (big vs little)

L1 — Recognition

Recall Solution 1.1

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 ). 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 ). 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.


L2 — Application

Recall Solution 2.1

Extract bytes ( is LSB): . Why: ; e.g. . Place little-endian (): 200:EF 201:BE 202:AD 203:DE.

Recall Solution 2.2

Big-endian reverses the index: . 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: (LSB), (MSB). Little-endian (): 10:FF 11:00. Big-endian (): 10:00 11:FF. Why matters: the big-endian index is , so the reversal length depends on the value's width.


L3 — Analysis

Recall Solution 3.1

In little-endian the byte at offset is byte index , so weight it by :

Recall Solution 3.2

In big-endian , so the byte at offset is the MSB. Weight the lowest address most: 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 as MSB: The value got byte-reversed — the classic bug fixed by ntohl in Network Protocols.


L4 — Synthesis

Recall Solution 4.1

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

has bytes , . Little-endian: lowest address holds b == 1. Big-endian: lowest address holds the MSB b == 0. The location of the single nonzero byte reveals the convention.

Recall Solution 4.3

Equivalently . 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.


L5 — Mastery

Recall Solution 5.1

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 ⇒ . Little-endian host, no conversion: treats offset 0 as LSB ⇒ . 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: . Read back by weighting each offset by : But is precisely the base-256 expansion of , so . The convention is lossless as long as write rule = read rule. (The same holds big-endian with .)


Level Map

L1 Recognition

L2 Application

L3 Analysis

L4 Synthesis

L5 Mastery

Spot the rule

Place the bytes

Read scrambled memory

Widths pointers protocols

Traps and proofs

Connections

  • Endianness (big vs little) — parent topic; all rules built there.
  • Memory Addressing — variable-width reads (Problem 4.1) rely on address ordering.
  • Data Representation — the value-to-bytes map .
  • Network Protocols — big-endian wire order, Problems 3.3 and 5.2.
  • Pointers and Type Punning — the detector cast in Problem 4.2.
  • Bitwise Operations — shift/mask construction in Problem 4.3.