Created on January 6, 2021 at 9:34 pm
Updated on January 6, 2021 at 9:34 pm

Which Endian Again?

I was going through these slides related to this talk about Go best practices when I got side tracked into researching endianness. Here is my best attempt at ELI5 for endianness.

Bits and Bytes

To drastically oversimplify memory addresses consist of 8 bit chunks called bytes that store up to 255 in numeric value. A byte always has it's number of highest significance at the left most bit which is bit 0.

11100111 = 231 where the lead 1 is the 2^7 value.

Most memory is allocated into anywhere from 16 bit, 32bit, or 64 bit memory chunks which is 2 bytes, 4 bytes, and 8 bytes respectively.

Endianness

This is where the concept of endianness comes in. Endianness is in which byte a machine stores the significant portion of a chunk of data. Big endian stores the significant chunk in the first most byte and little endian stores it the other way around

Now, endianness doesn't come into play in a 1 byte store of value, it only comes into play in a 2 or more byte store of value.

Let say we have this 2 byte number

00010001 10001000 = 4488 where the 1st byte equals 4352 and 2nd byte equals 136

The above format is in big endian, where the significant portion of the number is placed in the leading byte. Below is what little endian would look like

10001000 00010001 = 136 is the first byte and 4352 is the second

The hex equivalent of this is 1188 and 8811 respectively. You can try it out with the below Go code snippet.

package main

import (
	"fmt"
	"bytes"
	"encoding/binary"
)

func main() {
	buf := new(bytes.Buffer)

  	var data uint16 = 4488
	
	binary.Write(buf, binary.LittleEndian, &data)

	fmt.Printf("% x", buf.Bytes())
}

If you change the 2nd argument in the binary.Write method, it will give you either 1188 or 8811 depending on the endianness. Also if you change the uint16 to uint64 it will either give you a lot of leading or trailing zeros.

Reasoning Behind Each

My best guess for why a computer would set up little endian memory system is to truncate the trailing zeros if you, say, have a 2 byte number in a 4 byte memory location.

The big endian method is obvious as it's more human readable.

Thanks for reading!