Endianness
Endianness refers to the order in which bytes of a multi-byte structure are placed to main memory (RAM). The bit order in any byte is normally from bit seven down to bit zero (little-endian) and remains preserved.
Big- vs. little-endian
Big-endian stores a value in order, little-endian in reversed order of significance. In big-endian the most-significant (left-hand) byte, in little-endian the least-significant (right-hand) byte is stored at the lowest address.
The following table shows the byte number of a 32-bit structure with byte three equals bit 31 down to 24, byte two equals bit 23 down to 16, byte one equals bit 15 down to 8 and byte zero equals bit seven down to zero.
Memory address |
' n ' | n+1 | n+2 | n+3 | ||
---|---|---|---|---|---|---|
Big-endian: | 3 | 2 | 1 | 0 | ||
Little-endian: | 0 | 1 | 2 | 3 |
Little-endian may seem illogical until one considers that a variable can be used more easily as 8-bit, 16-bit, 32-bit or even 64-bit value without changing its base address. Therefore, AMD and Intel use little-endian order.
Big-endian is sometimes referred to as network byte order. Big endian order is used f.e. in natural languages or .png-files.
Examples
- Table
Memory address |
n | n+1 | n+2 | ... | ||
---|---|---|---|---|---|---|
Value = 0x15 |
Big-endian: | 0x15 | - | - | - | |
Little-endian: | 0x15 | - | - | - | ||
Value = 0x0A15 |
Big-endian: | 0x0A | 0x15 | - | - | |
Little-endian: | 0x15 | 0x0A | - | - | ||
Value = 0x780A15 |
Big-endian: | 0x78 | 0x0A | 0x15 | - | |
Little-endian: | 0x15 | 0x0A | 0x78 | - | ||
a.s.o. |
- Let's assume a x86-computer, which means little-endian order. Register eax contains the value 0xCAFEF00D. You store this register at address 0:
Memmory address: 0 contains: 0x0D
Memmory address: 1 contains: 0xF0
Memmory address: 2 contains: 0xFE
Memmory address: 3 contains: 0xCA
Now you read into register ax the value from address 0:
Register ax contains: 0xF00D
- Let's assume big-endian order. A 32-bit register contains the value 0xCAFEF00D. You store this register at address 0:
Memmory address: 0 contains: 0xCA
Memmory address: 1 contains: 0xFE
Memmory address: 2 contains: 0xF0
Memmory address: 3 contains: 0x0D
Now you read into a 16-bit register the value from address 0:
This 16-bit register contains: 0xCAFE
(Which is the upper-half of the value. You probably wanted the lower-half and therefor must change the base address and read from address: 2.)
- 578 Pound Sterling
"byte" from 9 to 0:
- big-endian: 578 £
- little-endian: 875 £
"byte" from 99 to 0:
- big-endian: 578 £
- little-endian: 7805 £
Common CPUs
CPU | Endianness |
---|---|
x86 | little |
x86-64 | little |
MIPS | both |
Motorola | big |
68k | big |
PowerPC | both |