Hello and welcome to another episode in the Godot basics tutorial series. In this episode we will be taking a look at Endianness. Before we begin let's talk about memory location. So when talking about memory location we generally mean that one memory location or basic storage unit stores one bite which again is eight bits. Now let's take a look at endianness. Endianness means the order or sequence of bytes in computer memory.
There are two types of endian this big endian or Little Endian no big endian means the most significant bite of digital data is that the smallest memory adjusts and the least significant bite is that the largest memory address little endian is basically the polar opposite of big endian when referring to little endian. What we mean is that the least significant bite of digital data is at the smallest memory address and the most significant bite is at the largest memory address before taking a look at and DNS.
Let's go ahead and take a look at the value we'll be using in the example. So in this case imagine we have a variable X which is equal to the value one million forty eight thousand five hundred seventy six. You can also think of that as two to the power of 20 and the hexadecimal representation is down here which is represented by 0 x 1 0 0 0 0 0. One thing we didn't go over in the last episode that I want to quickly go over in this episode is that the prefix is 0 x is just computer science notation that the value in front of it is a hexadecimal value.
So as an example appear since we're not using the 0 x prefix we can make the assumption that this value is in fact not a hexadecimal value but instead a decimal value. But no matter what the decimal value appears the same as the hexadecimal value down here on top of that I also have the binary representation of our hexadecimal as well as you can see here we have one two three four five zeros and as you can see here we have their binary form representation which again he checks the decimal value is for a bit.
So one two three four five and the one up here again keep in mind that two hexadecimal values make up one bite. And in this example we'll be looking over a value that is three bytes long. Now normally when dealing with integers we're either assigning four bytes in memory or two bytes in memory depending. But for this example just to keep things simple we're going to forego looking at four bytes and instead we'll just be looking at three bytes and how it would be placed in memory.
The next thing I want to talk about is bit order. Now you have your lease significant bit which means that when that bit has the value of one It represents a small value in our least significant bit and this example is the very first bit the value it represents is just one. Compare that to the bit all the way towards the left side of this example and that would be our most significant bit. And when this is one that value becomes two to the power of twenty three. Now Leslie I want to talk about bytes.
So we have three bytes one in blue one in Orange and the last in white to determine which byte is the least significant byte you've got to look at which byte houses the lowest exponent for the two value. So in this case zero is the lowest possible exponent for our two and because this byte contains the lowest possible exponent we call this the least significant byte towards the left with our byte again being in white towards the furthest on the left this bit represents two to the power of twenty three twenty three being the highest possible exponent possible for our two and because this byte contains the highest possible exponent we call this byte the most significant to bite and everything else in between is just in a spectrum.
I'm going to show you an example of big and little endian in regards to this hexadecimal value which again represents something in binary form when dealing with any type of endian. We have to remember that we're storing data in memory. Your computer has memory RAM for example. And whether it's an application like Safari or your good application where you're running your game your computer needs to allocate memory to data. And remember that each memory location can only hold up to one byte but sometimes your data needs more than one byte to represent some type of data.
But again your memory location can only support one bite at a time. So in this case we have memory address as 0 1 2 just to make it easier for us and zero is our first memory address location and two being the furthest. And again when dealing with little endian we want the least significant bite at the smallest memory address. And we would like the largest bite which again is the most significant bite at the largest memory address possible. And so if we were to take away our binary value and place each bite at a memory address location as you can see here are lowest or excuse me or least significant bite is that the lowest memory address or second least significant bite follows up next.
And our most significant bite is at the largest memory address. Now let's go ahead and compare that to Big endian. Now remember that when dealing with big endian or most significant bite will be at the lowest memory address which in this case is zero. And the least significant bite will be at the largest memory address possible. And so if we were to plug in our hexadecimal values in the big endian order what we get is that are again most significant bite is that the lowest memory address possible and the least significant bite is at the largest memory address possible.
Now the easiest way to remember the order of bytes is that when dealing with little endian byte order the little end starts first. And when dealing with big endian the big value starts first. Now no matter what our byte order is our value will still be the same. And so a big part of endian is that as long as we are consistent it should not matter how we are ordering things in memory address.
Now endian s can get quite complex so we won't go into a lot of detail. But your computer will either be big endian little endian or they can run both types but they can only run one type at a time. And so you do have a problem. How do you convert data from Big endian to little endian and well using different CPO architectures. You will have to do some type of conversion and this also applies to things such as networking we call this serialization. So what exactly is serialization.
Well serialization is the process of converting an object basically a data object into a stream of bytes to store that object to memory a database or file. And since we will be dealing with the file class let's go ahead and just do a quick and basic summary of serialization so serialization is quite a simple process. Basically you have your data object turn it into a stream of bytes and then you just populate either memory a database or file. And the idea of serialization is important because when you serialize something for example when you take your variable X which equals 42 and you serialized that data into a text file you do have the ability to store it in any type of order you want.
And so the order you store it in plays an important room because when another operating system reads from it for example they may read it in an order that it was never intended to be read from. For example if you write your variable which equals 42 into a file but you use big endian ordering and yet someone else reads from that file using little endian ordering you'll get different results. And so the way you stored data and the way you order your bytes and bits plays an important role well that's all for you in this episode.
Thank you so much for joining me. Thank you for clicking the Like button and thank you for clicking the Subscribe button. If you have any questions or comments please feel free to leave them in the comments section down below. I look forward to seeing you in the next episode. Have an amazing day