Endianness

From wikinotes

Endianness describes the order bytes are stored in memory in a multi-byte piece of data.
It has no effect on the order of the bits within a byte.

It is relevant both when managing memory, and when transmitting bytes over a stream (like networking).

  • Big Endian stores the bytes containing the larger numbers first.
  • Little Endian stores the bytes containing hte smaller numbers first.

Documentation

wikipedia https://en.wikipedia.org/wiki/Endianness

Basics

Endianness describes the order bytes are stored in memory in a multi-byte piece of data.

For example, the 2-byte(16-bit) integer 27591 is stored differently in big endian(BE) and little endian(LE).

# left-to-right (the big end of the number first)
BE: 01101011 11000111  # 0110101111000111

# right-to-left (the little end of the number first)
LE: 11000111 01101011  # 0110101111000111

Note that this does not describe the order of the bits in a byte, only the order of the bytes.

Terminology

background: If a large integer is stored in memory, it will require multiple bytes (8-bits).
For example, the number 4095 requires 12 bits.
In binary this would be 0000111111111111.
If described in 2x 8-bit bytes, this would be 00001111 11111111.

terminology

most significant byte: |
    byte containing the highest chunk of the number. 
    ex: 9876..... in 987654321

least significant byte: |
    byte containing the lowest chunk of the number.
    ex: ....54321 in 987654321

Pros/Cons

Little Endian

When summing a multi-byte integer in little endian, you can process the bytes in order,
applying the carry-over to the next byte. With big endian, you would need to fetch all
bytes, then fetch already obtained bytes again.

# little endian
   | 1:       | 2:       |3:      |
-- | -------- | -------- |--------|
a: | 11111111 | 00000000 |11111111|
b: | 11111111 | 00000000 |11111111|

# a1 + b1
  11111111  # a1
+ 11111111  # b1
----------
1 11111110  # sum, and 9th-bit w/ carryover 

# (carryover from a1/b1) + a2 + b2
  00000001  # carryover
+ 00000000  # a2
+ 00000000  # b2