Big Endian vs Little Endian explained
What you'll learn: You will understand how different computer architectures store multi-byte data types in memory.
When you store a large number like an int (usually 4 bytes) in memory, the computer has a choice to make: which part of the number goes into the first memory address? This choice is called Endianness.
The Mailbox Analogy
Imagine you have a long word like "COFFEE." You need to put it into three narrow mailboxes that only hold two letters each.
- Big Endian is like writing naturally: you put "CO" in the first box, "FF" in the second, and "EE" in the third. The "Big End" (the start) comes first.
- Little Endian flips it: you put "EE" in the first box, "FF" in the second, and "CO" in the third. The "Little End" comes first.
How it works in C
In C, a hexadecimal number like 0x12345678 is made of four bytes: 12, 34, 56, and 78.
- Big Endian: Stores the "most significant byte" (
0x12) at the lowest memory address. This is how humans usually read numbers. - Little Endian: Stores the "least significant byte" (
0x78) at the lowest memory address. Most modern processors (like Intel and AMD) use this method because it makes certain mathematical operations faster for the hardware.
Seeing it in code
You can see this in action by using a char pointer to look at the individual bytes of an integer:
#include <stdio.h>
int main() {
unsigned int num = 0x12345678;
unsigned char *ptr = (unsigned char *)#
// This prints the byte stored at the very first memory location
printf("The first byte in memory is: 0x%x\n", *ptr);
if (*ptr == 0x78) {
printf("Your system is Little Endian.\n");
} else {
printf("Your system is Big Endian.\n");
}
return 0;
}
Why does this matter?
Most of the time, C handles this for you. However, Endianness becomes critical when you send data over a network or save a binary file. If a Little Endian computer sends 0x12345678 to a Big Endian computer without converting it, the second computer will read the bytes in reverse and think the number is 0x78563412!
Key takeaway
Endianness is simply the order of bytes in memory: Big Endian starts with the most significant byte, while Little Endian starts with the least significant.