Are you familiar with Big and Little Endian, or have you heard about it somewhere along your programming journey but didn't pay much attention to it? In this short post, I will explain the concept in simple terms.
Endianness is the order in which bytes are stored and read in a computer's memory. It's a fundamental part of how computers understand and read bytes or any information input from the outside world.
When a computer needs to read data like this:
656669
If the computer reads the string of digits in Big Endian, it reads the most significant digits first. This means, 6, 5, 6, 6, 6, and 9, in that order. Therefore, the computer represents the data as the string of digits 65669
, translated as "sixty-five thousand six hundred sixty-nine" or whatever you, the user of computers, wish it to represent. For instance, if the computer is told to read the string into ASCII, you would get it to spell ABE
for you.
character | decimal | binary |
---|---|---|
A | 65 | 100 0001 |
B | 66 | 100 0010 |
E | 69 | 100 0101 |
On the other hand, a small endian computer read the least significant digits first, meaning 9, 6, 6, 6, 5, and 6. This is a different string of digits, 966656
.
In reality, computers only read in bits and bytes (8 bits). For example, if we are to read the integer 330
into a computer program in Big endian, it would begin with the true bit representation of the integer:
integer | binary (in 2-byte word) |
---|---|
330 | 0000 0001 0100 1010 |
integer | MSB | LSB |
---|---|---|
330 | 0x01 | 0x4A |
A Big-endian computer (or program) will read the most significant bytes first, which is 0x01
followed by 0x4a
. So you end up getting the exact readable order of the bit string.
A small-endian one, on the other hand, will read the least significant bytes first, which is 0x4A
then 0x01
.
In order to retrieve the right data, we have to know if a computer reads in which endianness. That's because you can get different data from when it was read. (0x014A
= 330 while 0x4A01
= 18945).
Here is a very simple test in Rust to prove it. We simply use the standard methods of a 16-bit (or 2-byte) unsigned integer type in Rust to read two-byte arrays which we intentionally swap the byte places.
fn test_both_endians() {
let a = u16::from_be_bytes([0x01, 0x4a]);
let b = u16::from_le_bytes([0x4a, 0x01]);
assert!(a == b);
}
Endianness starts to become handy once you have to build a program that serializes and deserializes data. The concept is very simple. Let me know if you have any questions.
Top comments (0)