Endian Format

Storing Words in Memory

We've defined a word to mean 32 bits. This is the same as 4 bytes. Integers, single-precision floating point numbers, and MIPS instructions are all 32 bits long. How can we store these values into memory? After all, each memory address can store a single byte, not 4 bytes.

The answer is simple. We split the 32 bit quantity into 4 bytes. For example, suppose we have a 32 bit quantity, written as 90AB12CD16, which is hexadecimal. Since each hex digit is 4 bits, we need 8 hex digits to represent the 32 bit value.

So, the 4 bytes are: 90, AB, 12, CD where each byte requires 2 hex digits.

It turns out there are two ways to store this in memory.


Big Endian

In big endian, you store the most significant byte in the smallest address. Here's how it would look:

AddressValue
100090
1001AB
100212
1003CD

Little Endian

In little endian, you store the least significant byte in the smallest address. Here's how it would look:
AddressValue
1000CD
100112
1002AB
100390

Notice that this is in the reverse order compared to big endian. To remember which is which, recall whether the least significant byte is stored first (thus, little endian) or the most significant byte is stored first (thus, big endian).

Notice I used "byte" instead of "bit" in least significant bit. I sometimes abbreciated this as LSB and MSB, with the 'B' capitalized to refer to byte and use the lowercase 'b' to represent bit. I only refer to most and least significant byte when it comes to endianness.

Code for Endianess

1. Little Endian
This code is in C++
Consider,
n is 32 bit

unsigned long LittleEndian(unsigned long n)
{
n=(n>>24)|  //move first byte to last
 ((n<<8)&0x00ff0000)  //move 3rd to 2nd
 |((n>>8)&0x0000ff00) //move 2nd to 3rd
 |(n<<24);   //move last to first
return n;
}

2. Big Endian

Actually, no need of having function for Big Endian.
When you use numbers in your code, they are actually in Big Endian Format as your MSB is at smallest address.