Endianness
Learn about the little-endian and big-endian byte orders used by computers.
Introduction
We know that different data types have different sizes and that we typically measure that size in bytes.
For example, an integer has a size of 4 bytes. Therefore, to represent the integer in memory, we use a sequence of 4 bytes or 32 bits. Let’s say we want to represent the integer 2022
. The binary representation of 2022
is 11111100110
.
We need 11 bits to represent it, but we know an integer takes 32 bits, so to extend 11 bits to 32, we add 0
s as padding. Therefore, the full representation is 00000000000000000000011111100110
. Or, if we want to see the individual bytes, we can add some spacing after each group of 8 bits: 00000000 00000000 00000111 11100110
.
Let’s create a memory drawing and represent the bytes:
Notice that we unconsciously made an assumption and placed the bytes in memory in the same order we wrote them on paper or inside the lesson. But who is to say this is the correct way of ordering them?
Why not place them like in the following image?
It turns out that both representations are valid, depending on the architecture. The order in which we align the bytes (not the bits) is called endianness.
MSB and LSB
To define endianness, we need to discuss the concepts of the most significant byte (MSB) and least significant byte (LSB).
Let’s ignore the binary aspect and look at the decimal number 69875432. The leftmost digit (6) will be the MSB, and the rightmost digit (2) will be the LSB. If we change the 6, we create the most significant impact inside the number. If we change the 2, we make the lowest impact inside the number.
- Say we change 6 to 1, and the number becomes 19875432. The difference between 69875432 and 19875432 is 50000000. It means the number changed a lot when we changed the MSB.
- Say we change the 2 to 1, and the number becomes 69875431. The difference between 69875432 and 69875431 is 1, which means the number barely changed when we changed the LSB.
Now, let’s consider the binary number 1111111111111111
, which is 65535 in decimal. The MSB and LSB are (MSB)11111111
and (LSB)11111111
.
- If we discard the MSB, we get
00000000 11111111
, which is 255 in decimal. Notice how the number changed from 65535 to 255 by discarding the MSB. - If we discard the LSB, we get
11111111 00000000
, which is 65280 decimal. Notice the number barely changes when we discard the LSB. - In both cases, we discard the same byte (