What is big-endian and little-endian?
Why reverse the byte storage order?
The articles written on this blog were created with the goal of teaching. Also, the information in the article may be incorrect, so please be careful.
While coding, you may have sent data over the network or read a specific file. There are cases where the value comes out completely different from what I expected. The official name for this phenomenon is Endianness arrangement method. That is, they are called Big Endian and Little Endian.
When the CPU stores 4-byte int-type data in memory, it does not pack it in as a whole, but breaks it into 1-byte chunks and stores them in the address in order. At this time, it depends on whether you put it in order starting from the largest digit (MSB) or in reverse order starting from the smallest digit (LSB). The way data is placed in memory is completely different.
If you put the numbers in an intuitive order, as if a person reads them, they are big-endian, and if you put them in reverse order to make it easier for machines to calculate them, they are little-endian. Most modern PCs, such as the Intel or AMD CPUs we mainly use, adopt the little endian system.
Let’s check it ourselves with the code.
#include <iostream>
#include <iomanip>
int main() {
//4 byte hexadecimal integer data
unsigned int number = 0x12345678;
//Casting a pointer to check how each byte is stored in memory
unsigned char* bytePtr = reinterpret_cast<unsigned char*>(&number);
for (int i = 0; i < sizeof(int); ++i) {
std::cout << "&bytePtr[" << i << "] : 0x"
<< std::hex << std::setw(2) << std::setfill('0')
<< (int)bytePtr[i] << std::endl;
}
/*
* Based on little endian system
* &bytePtr[0] : 0x78 //Smallest unit (LSB) goes into lowest address first
* &bytePtr[1] : 0x56
* &bytePtr[2] : 0x34
* &bytePtr[3] : 0x12
*
* Based on big endian system
* &bytePtr[0] : 0x12 //The largest unit (MSB) goes into the lowest address first.
* &bytePtr[1] : 0x34
* &bytePtr[2] : 0x56
* &bytePtr[3] : 0x78
*/
return 0;
}
A question arises here. The same value is stored anyway, Why did they adopt an unintuitive method of storing data upside down, like little endian? This is due to the design of the adder inside the CPU and the efficiency of type casting.
When the CPU adds or subtracts two numbers, it must calculate from the 1's place, that is, the smallest digit, in order to perform carry. In little endian, the smallest unit (LSB) is located at the first address in memory, As soon as the CPU reads the memory, it can immediately start performing arithmetic operations. Also, when downcasting by truncating a 4 Byte integer to 1 Byte (char), Hardware logic is greatly simplified because only the starting address of the memory needs to be extracted without additional address calculation.
On the other hand, big-endian is intuitive for humans to read when looking at the memory dump, When comparing the size of two numbers, it has the advantage of being compared from the first digit. So, to avoid confusion when exchanging data, In network standard communications such as Internet Protocol (TCP/IP), big-endian is used as a standard.