5.1.11 · D3Instruction Set Architecture (ISA)

Worked examples — Endianness (big vs little)

2,383 words11 min readBack to topic

Before we start, one reminder of the two symbols we lean on the whole way down.


The scenario matrix

Endianness looks tiny, but the questions about it fall into distinct cells. Below is every class of case. The worked examples afterwards each tag which cell they cover, and together they fill the whole grid.

Cell Scenario class What makes it tricky
C1 Ordinary 4-byte int, both endians The baseline placement
C2 2-byte (16-bit) value Smaller — index range changes
C3 8-byte (64-bit) value Larger — bookkeeping stress
C4 Zero value 0x00000000 Degenerate: both endians look identical
C5 Palindromic bytes e.g. 0x12341234? (partial symmetry) Which bytes actually move?
C6 Value with a 0x00 in the middle / leading zeros Reading fewer bytes; sign-of-location
C7 Negative int (two's complement, high bit set) Sign bit lives in the MSB — where does it go?
C8 A char[] string (not a scalar) Endianness does not apply — the trap
C9 Reading back memory → value (inverse direction) Reconstruct from bytes
C10 Cross-endian transmission (network byte order) Real-world word problem
C11 Type-punning twist: read a 4-byte int as two 2-byte halves Exam-style; sub-word views
C12 Mixed-type struct laid out in memory Each field endian-swaps independently; padding

We now walk them all.


The worked examples

Baseline placement

Figure — Endianness (big vs little)

Different value sizes

Figure — Endianness (big vs little)
Figure — Endianness (big vs little)

Degenerate & near-degenerate inputs

Figure — Endianness (big vs little)

The sign bit

The classic trap: strings are not scalars

The inverse direction

Real-world word problem

Sub-word views (type-punning)

Mixed-type struct

Figure — Endianness (big vs little)

Coverage check

Recall Did we fill every cell of the matrix?

C1 baseline ::: C1 example (0x0A0B0C0D) C2 16-bit ::: C2 example (0xBEEF) C3 64-bit ::: C3 example (0x11..88) C4 zero degenerate ::: C4 example (0x00000000) C5 partial symmetry ::: C5 example (0x12341234) C6 leading zeros / narrow read ::: C6 example (0x00000005) C7 negative / sign bit ::: C7 example (-2 = 0xFFFFFFFE) C8 string not scalar ::: C8 example ("Helo") C9 read back → value ::: C9 example (0x1234CDAB) C10 network transmission ::: C10 example (port 443) C11 sub-word type-pun ::: C11 example (0xAABBCCDD halves) C12 mixed-type struct ::: C12 example (a/b/c with padding)


Connections

  • Endianness — parent topic — the core rule these examples exercise.
  • Data Representation — the decomposition every example rests on.
  • Memory Addressing — offsets and low/high addresses used throughout.
  • Network Protocols — C10's big-endian "network byte order" and htons.
  • Pointers and Type Punning — C11's sub-word reinterpretation and C12's struct layout.
  • Bitwise Operations — the shift/mask that implements byte extraction.
  • Instruction Set Architecture (ISA) — the CPU commits to one endianness.