Memory - Byte Order - (Endian-ness) - Storing (multi-byte) Word in memory

1 - About

Storage of a word in memory of more than one byte.

If each memory address of the memory array can store only a single byte, you need to split the word in multi-part one byte long.

This part are stored in memory in an order called Endianness:

If the first byte store:

  • the most significant byte (MSB): big endian - natural order - aka network (byte) order
  • the least significant byte (LSB): little endian

Endianness only makes sense when you are breaking up a multi-byte word, and attempting to store the bytes at consecutive memory locations.

3 - Example

Storage of the hexadecimal word 90AB12CD

3.1 - Big endian

Address Value (In Hexadecimal)
1000 92
1001 AB
1002 15
1003 CD

3.2 - Little Endian

Address Value (In Hexadecimal)
1000 CD
1001 15
1002 AB
1003 92

4 - CPU

Different instruction set architecture (ISAs) (and then machine) use different endianness.

  • big endian: Motorolas and Suns
  • little endian: DEC and IBMs

5 - Problem with endianness

If you (send|transfer) data from a source machine of one endianness to a target machine of the opposite endianness, the target machine will read badly the data in reversed order.

5.1 - Solution

The solution is to send 4 byte quantities using network byte order which is arbitrarily picked to be big endian. If the target machine has the same endianness as network byte order, then great, no change is needed. If not, then you must reverse the bytes.

6 - History of Endian-ness

In the book “Gulliver's Travels”, Jonathan Swift talks about how:

  • certain people prefer to eat their hard boiled eggs from the little end first (thus, little endian),
  • while others prefer to eat from the big end (thus, big endians)

and how this lead to various wars.

Of course, the point was to say that it was a silly thing to debate over, and yet, people argue over such trivialities all the time.

For example:

  • should braces line in parallel or not?
  • vi or emacs?
  • UNIX or Windows

7 - Documentation / Reference

data_storage/endian.txt · Last modified: 2017/04/19 21:13 by gerardnico