Skip to content

Instantly share code, notes, and snippets.

@foxx
Created December 27, 2024 11:08
Show Gist options
  • Save foxx/3cfba8c4e05584a6f3aa7614e609bc1a to your computer and use it in GitHub Desktop.
Save foxx/3cfba8c4e05584a6f3aa7614e609bc1a to your computer and use it in GitHub Desktop.
A tail of endianness

The Hilarious History and Reality of Endianness

The College Version πŸŽ“

So there was this absolute legend named Jonathan Swift who wrote "Gulliver's Travels" back in 1726. In the book, there's this ridiculous war between two groups over which end of an egg you should crack first - the big end or the little end. It was basically Swift roasting politicians for fighting over dumb stuff.

Fast forward to the 1970s-80s, and computer architects are like "yo, how should we order bytes in memory?" You've got two main options:

Big-endian: 
0x12345678 stored as: 12 34 56 78
(like reading left to right, most significant byte first)
Think: IBM mainframes, early RISC, PowerPC, SPARC

Little-endian:
0x12345678 stored as: 78 56 34 12
(like reading right to left, least significant byte first)
Think: Intel x86, AMD, most modern PCs

Intel went with little-endian because it made decimal math easier (you can add numbers starting from the least significant digit, just like how we do math on paper). It also let them do some neat tricks with memory addressing.

The drama peaked when the internet protocols were being designed. They had to pick one format for network communication, so they went with big-endian (calling it "network byte order"). This means your Intel PC has to flip all its bytes around when sending data over the network - that's why you see those htons() (host-to-network-short) functions in networking code.

The Cat Version 🐱

Imagine you have a cat food bowl with 4 sections (like our 4 bytes). Each section has different treats:

Treats = 0x12345678

Big-endian (formal cat):
[12] [34] [56] [78]
 🐟   🐀   🐁   🦐
"I shall eat my meal from left to right, as is proper"

Little-endian (chaotic cat):
[78] [56] [34] [12]
 🦐   🐁   🐀   🐟
"I'll start with the shrimp because I WANT IT NOW!"

The mainframe cats (like IBM) were very proper and dignified, so they preferred big-endian: "One must start with the fish, then the bird, followed by mouse, and finally the shrimp. It's only proper!"

But then came the Intel cats, who were like "EXCUSE ME but I see shrimp and I'm eating that FIRST!"

The Hilarious Reality of Numbers vs Text

Let's take the number 0x12345678 and the text "CATS"

NUMBER REPRESENTATION:
--------------------
Memory addresses:    0   1   2   3
Big Endian:         12  34  56  78    "I am very proper!"
Little Endian:      78  56  34  12    "olleH backwards am I!"

TEXT REPRESENTATION:
------------------
Memory addresses:    0   1   2   3
ASCII "CATS":       43  41  54  53    
Hex:                C   A   T   S     
                    
The hilarious part:
- Big Endian:    "CATS" is stored as... "CATS"  🐱
- Little Endian: "CATS" is stored as... "CATS"  🐱

WAIT, WHAT? 
Text stays the same in both?! 
YES! Because text is treated as a sequence of bytes, not a single multi-byte value!

Modern vs Old CPU Performance

OLD CPUS (1980s-90s):
--------------------
Big Endian CPU handling big endian:    FAST     "I was born for this!"
Little Endian CPU handling little:     FAST     "Weee!"
Converting between them:               SLOW     "Oh god, byte shuffling... *wheeze*"

MODERN CPUS (Today):
------------------
Big Endian CPU handling big endian:    FAST     "Still got it!"
Little Endian CPU handling little:     FAST     "Zoom zoom!"
Converting between them:               FAST*    "I have special instructions for this!"

* Modern CPUs have dedicated instructions like:
- BSWAP (byte swap)
- MOVBE (move with byte swap)
- PSHUFB (shuffle bytes however you want)

The Probability of Networking Switching to Little Endian

P(switching to little endian) = 
    (# of TCP/IP protocols that could change) / 
    (# of devices that would need updating) * 
    (likelihood of global agreement) *
    (probability of breaking everything)

= 0% * ∞ * 0% * 100%

= HAHAHAHAHAHA NO

Why it's never happening:

  1. Legacy Hardware:

    Router from 1995: "I still work perfectly!"
    Network Admin: "If it ain't broke..."
    
  2. Standards Inertia:

    RFC 1234: "Network byte order is big endian"
    Internet: "Been that way since 1981!"
    Developer: "But maybe we could..."
    Entire Internet: "NO TOUCHING!"
    
  3. Coordination Problem:

    Step 1: Get every network device manufacturer to agree
    Step 2: Get every OS vendor to agree
    Step 3: Get every network admin to agree
    Step 4: Wake up from this impossible dream
    
  4. The Great Flag Day:

    Last time we tried switching protocols (IPv4 to IPv6):
    Started: 1990s
    Status in 2024: "Still working on it!"
    

The probability is somewhere between:

  • Getting everyone to agree on tabs vs spaces
  • Making JavaScript predictable
  • Teaching cats to respect personal boundaries

In other words: We'll probably colonize Mars before networking switches to little endian! 😹

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment