Your Computer Is Reading This Backwards

June 17, 20269 min readComputer ScienceComputer ArchitectureExploring C

Imagine you write 305,419,896 on a piece of Paper.

Most people think its just a number, but the same number in your computer's memory would be stored backwards.

1. The Basics

Computers don't store large numbers as a single chunk. Instead, they split them into smaller pieces called bytes.

Lets take the hexadecimal number 0x12345678 which is 305,419,896 in base 10.

Since each pair of hexadecimal digits represents one byte, this number consists of four bytes:12 34 56 78

Now, the order in which these bytes are stored in memory is what we call endianness.

Big-Endian

Reads the "Big End" (Most Significant Byte) first.

0x120x340x560x78...

Little-Endian

Reads the "Little End" (Least Significant Byte) first.

0x780x560x340x12...

2. Why Do We Even Have This?

Little Endian sounds absurd at first.

The answer lies in the early days of computing and the trade-offs engineers had to make. There are two main reasons:

  • Arithmetic starts with the least significant byte (addition, subtraction, carry propagation). Will be writing an article on computer arithmetic and the ALU soon.
  • Integers are easier to work with because the lowest-address byte always contains the least significant part of the value. For example, the 32-bit value 0x0000000C which is (12)10 is stored as 0C 00 00 00 in little-endian memory. This means the least significant byte can be accessed directly by reading the first byte in memory. In a big-endian system (00 00 00 0C), the least significant byte is located at the highest address instead. While modern processors can handle either arrangement efficiently, storing the least significant byte first simplified some arithmetic and variable-sized integer operations in early computer designs.

In The End Little Endian was seen as a more practical and efficient choice for early microprocessor designs, and its advantages in handling variable-length data and simplifying arithmetic operations helped solidify its position.

3. The Endianness War

Different computer architectures chose different approaches.

Historically

  • Many Motorola processors used big endian.
  • Intel processors used little endian.
  • Some architectures could operate in either mode. Such as ARM, PowerPC, and MIPS. RISC-V is primarily little-endian, although some extensions and profiles have introduced support for big-endian operation.

As computers became more connected, this created a problem. What happens when a big-endian machine sends data to a little-endian machine? Without an agreed-upon format, the bytes could be interpreted incorrectly.

To solve this problem, internet protocols standardized on big endian, often called network byte order. But Why Big Endian? The answer is again backwards compatability 🙃, when the standard was set the predominant computers where following Big-Endiannes, so it made sense to make the protocol they communicate through Big-Endian. Another reason is debugging is easier with Big-Endian because it is how a human reads.


So the next time you see 78 56 34 12 in a memory dump, remember: your computer probably isn't confused. It's just speaking little-endian.