Bite-Sized Byte Order

A byte order crash course.

Allek Mott
The Startup
5 min readSep 30, 2019

--

Byte order concerns the way bytes are organised in memory. Specifically, it describes the sequence in which bytes will appear. It is a core concept in memory organisation and structures, and, as such, it sometimes peaks into more general lower level programs.

Today, I’ll go over some byte order basics, focusing on little and big endian orderings. This is best read with a basic knowledge of binary, hexadecimal, and bytes.

Words

Photo by Brett Jordan on Unsplash

Byte order revolves around words. And no, not like the ones I’m typing right now, though they are similar. These words, rather, are fixed-length groups of bytes.

We’re concerned with how the bytes are ordered in each of these words.

Why do words matter?

Words are the key organisational unit of memory. A processor’s bit length (32-bit, 64-bit) can hint at the standard word size (4- and 8- bytes respectively) it uses in organising memory.

Since memory is made up of words, word byte ordering can have system-wide effects and make for significant design decisions.

Endianness

Endianness is a term used to denote the order of bytes in words. The two most common kinds of endianness are big and little.

Byte Significance

The primary decider of endianness is significance, which has to do with a byte’s place in the word.

Let’s see how significance works with decimal numbers. Looking at the number 1337:

  • The digit 1 at the beginning would be considered the most significant, since its place has the most weight (thousands place).
  • 7, similarly, is the least significant, as its place carries the least weight (ones).

This works the same way in binary words.

Looking at the 4-byte hexadecimal number 54 63 65 20:

  • The byte 54 is the most significant, since it’s place has the most weight (2^24).
  • The byte 20 is the least significant, since it’s place has the least weight (1).

Big Endian

If a word is big-endian, the most significant byte appears first, followed by second most, etc., until the last, it being the least significant.

Here’s the hex word 54636520 with big endian byte ordering: 54636520 (the same).

Little Endian

In contrast, little endian implies the reverse — the least significant byte appears first, with the most significant appearing last.

Here’s 54636520 with little-endian byte ordering: 20656354.

Example: Applying Byte Order to English

Photo by Amador Loureiro on Unsplash

Let’s convert an English sentence into its big- and little-endian representations. We’ll follow the rules of English here, meaning words can be as long as they want. Each letter (or punctuation mark) corresponds to one byte.

Here’s a big endian sentence:

The cow jumped over the moon.

And that same sentence, but in little endian:

ehT woc depmuj revo eht .noom

As you can see, the word order is preserved. The word letter order, on the other hand, is reversed.

Applying Byte Order Where It’s Supposed to Be

Ok, time for the grand finale. We’ll use the same sentence from the previous section, but instead of using the letters as bytes, we’ll use the actual bytes representing each character in the sentence.

Here’s that sentence alongside its bytes’ hex values:

T  h  e     c  o  w     j  u  m  p  e  d     o 
54 68 65 20 63 6f 77 20 6a 75 6d 70 65 64 20 6f
v e r t h e m o o n .
76 65 72 20 74 68 65 20 6d 6f 6f 6e 2e 00 00 00

Note that, as would also happen on the computer, I’ve added empty padding bytes at the end to bring our byte total to an even (divisible by 4) 32. Otherwise, the last word would be partial, which can be a bit awkward to deal with.

Unlike in English, we’re bound to computer law now, which means our words have a fixed length. Let’s assume they’re 32-bit (4 byte) words.

That means instead of the pretty, well-spaced English words we had previously, we now have 8 uglier words, spliced by spaces:

T  h  e        c  o  w
54 68 65 20 63 6f 77 20
j u m p e d o
6a 75 6d 70 65 64 20 6f
v e r t h e
76 65 72 20 74 68 65 20
m o o n .
6d 6f 6f 6e 2e 00 00 00

Can you guess the big endian version of our new sentence? It’s the same as above.

The little endian, on the other hand:

   e  h  T        w  o  c
20 65 68 54 20 77 6f 63
p m u j o d e
70 6d 75 6a 6f 20 64 65
r e v e h t
20 72 65 76 20 65 68 74
n o o m .
6e 6f 6f 6d 00 00 00 2e

ehT dnE

I was tempted to make the end a pun too, but I’m sure you’ve caught on by now.

Today, we learned that byte order concerns how bytes are ordered in words. If big endian ordering is used, bytes appear in each word as digits appear in a decimal number: highest weight to lowest weight. Contrastingly, if little endian is used, we see the reverse order: lowest weight to highest weight.

I hope this has been a useful foundation for future memory-related shenanigans. Thanks for reading!

--

--