Endianness Explained in Less Than 9 Minutes
I had a few people ask me to explain endianness in computing, because they found it confusing
- They knew that some processors are little-endian, while others are big-endian, but couldn't get their heads around what that means, and how to deal with it.
So, I'm going to explain what endianness is, why you probably own't need to worry about it, and a few other juicy tid-bits. Unlike others, I'm NOT going to explain it in terms of an egg, because we're not in Liliput, and Gulliver hasn't travelled here.
What is Endianness?
It's all about how large numbers are stored in memory.
Lets say we have the number 12,345. To us, 12345 makes instant sense. You start with the largest digit, and write the numbers from left to right. Whether you knew it or not, this is a big-endian format because the biggest value, i.e., the Most-Significant-Digit, appears first.
However, you could also write the number with the smallest value first, that is the Least-Significant-Digit, in which case it would read 543,21.
Confusing? Only because you're used to seeing all numbers in big-endian format. If everyone agreed to write numbers in little-endian, then you'd be used to it, and it would be big-endian numbers that were confusing.
Endianness in Computing
In the computer world, we have CPUs that are big-endian, and CPUs that are little-endian. Heck, we even have CPUs that are considered bi-endian.
Why? Why on earth do we have to put up with different endianness in the first place. Well, back the 60s and 70s (so, ancient computing times), there were very few standards in computing, and there were few companies competing with each other for global computing dominance.
As any good entrepreneur knows, you need to differentiate yourself from your competition. So, if one company did things one way, then the other would do things the other way to differentiate themselves from their competitiors, and then "explain" why their way was better (even when it wasn't).
Companies like Intel went with little-endian in their x86 CPUs, which ended up in PCs. Others like Motorola used big-endian in their 68000 series, which ended up in the original Apple Macintosh and Commodore Amiga.
And now we're stuck with competing standards...
Endianness in Computing
Let's take a closer look at how this is done in computers. With computers, numbers are stored in a binary format, with a byte being the basic unit. A byte can store a number between 0 and 255, or -128 to 127. It can also represent all the letters of the alphabet and more in ASCII.
Everything is fine so long as your numbers stay within those ranges.
But 0-255 is too small. How can we store larger numbers? Obviously we're going to have to use multiple bytes to store it.
A 16-bit number takes up 2 bytes, and can store numbers up to 65,535. 32-bits (or 4 bytes) can store numbers up to 4,294,967,296. And 64-bits can store insanely huge numbers.
Here's where endianness comes into play. In what order do we store these bytes? Let take the number: 324,508,639. This is 0x13579BDF in hexadeximal format. We use hexadecimal, because two hex digits fit perfectly into a byte.
In a big-endian system, the bytes are stored in the order that we naturally write them, with the Most-Significant-Byte first: 13 57 9B DF. With little-endian systems, the Least-Significant-Byte comes first, putting it in the order DF 9B 57 13.
And that, in an egg-shell, is endianness. It's simply the order that bytes of large numbers are stored in memory.
Origin of the Name Endianness
The name endianness was inspired by Jonathan Swift's classic novel Gulliver's Travels, in which the tiny people of Liliput and Blefescue have a war over whether eggs should be eaten from the little end, or the big end. Little enders, big enders, little endian, big endian. Get it?
It Could Have Been Worse
Anyway, be grateful that the big companies only competed on byte ordering, because imagine having to cope with different bit ordering within the bytes too, so 4 different combinations. Now that, would have made our brains hurt...
No Need To Worry
While this is all very interesting, as a programmer you're unlikely to have to worry about it, most of the time. The CPU reads and writes numbers in its native endian format automatically, and, so long as your code doesn't need to interact with data from another system, then everything is in the same endian format, and it all "just works."
You only need to worry about endianness in specific circumstances:
- Transferring data between computer hardware of different endianness, which can include peripheral devices (e.g., my AmigaOS graphics drivers have to interface a big-endian PowerPC CPU with little-endian GPU)
- Transferring between computers on a network (TCP/IP uses big-endian)
- Loading and saving data from disk (if the file format uses a different endianness to the CPU)
- When peeking at memory in a debugger on little-endian systems
That last point is because you look at memory as a big array, and need to mentally do the endianness byte swapping. The computer cannot swap the endianness automatically without knowing how many bytes make up a number.
For example, converting this little-endian number (DF 9B 57 13) to big-endian only works correctly if you know that it's a 32-bit number (13 57 9B DF). Treat it as two 16-bit numbers, and you get: 9B DF 13 57, which is a totally different number.
Endianness is Fading
If you still find endianness confusing, annoying, or both, then I have great news for you: Endianness is likely to become less and less important, because little-endian is now dominant, courtesy of the x86 CPU architecture's dominance. It's so dominant that Linux for PowerPC is switching to little-endian. PowerPC can handle both big and little endian, but maintaining drivers for big-endian CPUs in an increasingly little-endian computing world was getting too heavy, and the easiest solution was to switch the CPU's endianness to match the attached hardware.
What's Next?
So, now you understand endianness. But how do you convert between little and big endianness? That's the topic for my next video in this series.
Post your comment