1. Big Endian (The “Natural” Way)
- MSB first & LSB last as we normally write
Memory Layout for 0x12345678:
| Address | Value |
|---|---|
0x100 | 12 |
0x101 | 34 |
0x102 | 56 |
0x103 | 78 |
2. Little Endian (The “Reverse” Way)
Little Endian stores the LSB first (at the lowest memory address). Most Intel/AMD processors (x86 architecture) use this.
Memory Layout for 0x12345678:
| Address | Value |
|---|---|
0x100 | 78 |
0x101 | 56 |
0x102 | 34 |
0x103 | 12 |
How to Check
We can inspect the first byte of an integer to see what lives there.
If you are on a Little Endian machine, the value of *c be ``
#include <stdio.h>
int main() {
unsigned int x = 1; // In hex: 0x00000001
char *c = (char*) &x; // Point a byte-pointer to the start of the integer
printf("First byte value: %d\n", *c);
return 0;
}