"big-endian" and "little-endian" are terms that describe the order in which bytes are arranged within a binary representation of a number.
- Big-endian: The most significant byte (the "big end") is stored at the smallest memory address.
- Little-endian: The least significant byte (the "little end") is stored at the smallest memory address.
Here's a simple C++ example to illustrate big-endian byte order:
-
Understand the concept: Let's consider the integer
0x12345678
. In big-endian order, this integer is stored in memory as:Address Value 0x00 12 0x01 34 0x02 56 0x03 78
-
Code Example: Below is a simple C++ program that shows how to determine if your system is big-endian and how to manually interpret an integer as big-endian.
#include <iostream>
// Function to check if the system is big-endian
bool isBigEndian() {
unsigned int test = 1;
char *byte = (char*)&test;
return byte[0] == 0;
}
// Function to print bytes of an integer
void printBytes(int value) {
unsigned char *bytes = (unsigned char*)&value;
for (size_t i = 0; i < sizeof(int); ++i) {
std::cout << std::hex << (int)bytes[i] << " ";
}
std::cout << std::dec << std::endl;
}
int main() {
int number = 0x12345678;
std::cout << "System is " << (isBigEndian() ? "Big-endian" : "Little-endian") << std::endl;
std::cout << "Original integer: 0x" << std::hex << number << std::dec << std::endl;
std::cout << "Bytes in memory: ";
printBytes(number);
return 0;
}
-
isBigEndian Function:
- We create an unsigned integer
test
with a value of1
. - We then cast the address of
test
to achar*
(pointer to a byte). - If the first byte (
byte[0]
) is0
, it means the most significant byte is at the lowest address, indicating a big-endian system.
- We create an unsigned integer
-
printBytes Function:
- This function prints out the bytes of an integer.
- We cast the address of the integer to an
unsigned char*
to access individual bytes. - We then print each byte in hexadecimal format.
-
main Function:
- We define an integer
number
with the value0x12345678
. - We check and print whether the system is big-endian or little-endian.
- We print the original integer in hexadecimal format.
- We call
printBytes
to print the bytes of the integer as they are stored in memory.
- We define an integer
When you run this program, it will tell you whether your system is big-endian or little-endian and show you the byte order of the integer 0x12345678
in memory.
In a little-endian system, the least significant byte is stored at the smallest memory address. For the integer 0x12345678
, this means that the bytes are stored in reverse order compared to a big-endian system.
Here’s how 0x12345678
would be stored in memory on a little-endian system:
Address Value
0x00 78
0x01 56
0x02 34
0x03 12
Let's update the previous C++ example to include a demonstration of this. We'll add a function to print the bytes in both big-endian and little-endian formats to make the difference clear:
#include <iostream>
// Function to check if the system is big-endian
bool isBigEndian() {
unsigned int test = 1;
char *byte = (char*)&test;
return byte[0] == 0;
}
// Function to print bytes of an integer in the current system's endian format
void printSystemBytes(int value) {
unsigned char *bytes = (unsigned char*)&value;
for (size_t i = 0; i < sizeof(int); ++i) {
std::cout << std::hex << (int)bytes[i] << " ";
}
std::cout << std::dec << std::endl;
}
// Function to print bytes of an integer in big-endian format
void printBigEndianBytes(int value) {
unsigned char *bytes = (unsigned char*)&value;
for (int i = sizeof(int) - 1; i >= 0; --i) {
std::cout << std::hex << (int)bytes[i] << " ";
}
std::cout << std::dec << std::endl;
}
int main() {
int number = 0x12345678;
std::cout << "System is " << (isBigEndian() ? "Big-endian" : "Little-endian") << std::endl;
std::cout << "Original integer: 0x" << std::hex << number << std::dec << std::endl;
std::cout << "Bytes in system's endian format: ";
printSystemBytes(number);
std::cout << "Bytes in big-endian format: ";
printBigEndianBytes(number);
return 0;
}
-
printSystemBytes Function:
- This function prints out the bytes of an integer as they are stored in the current system's endian format.
- It accesses the bytes by casting the address of the integer to an
unsigned char*
and prints each byte in hexadecimal format.
-
printBigEndianBytes Function:
- This function prints the bytes of the integer in big-endian format, regardless of the system's actual byte order.
- It iterates through the bytes in reverse order to simulate big-endian storage.
-
main Function:
- Defines an integer
number
with the value0x12345678
. - Checks and prints whether the system is big-endian or little-endian.
- Prints the original integer in hexadecimal format.
- Calls
printSystemBytes
to print the bytes in the system's endian format. - Calls
printBigEndianBytes
to print the bytes in big-endian format.
- Defines an integer
On a little-endian system, the output might look like this:
System is Little-endian
Original integer: 0x12345678
Bytes in system's endian format: 78 56 34 12
Bytes in big-endian format: 12 34 56 78
On a big-endian system, the output might look like this:
System is Big-endian
Original integer: 0x12345678
Bytes in system's endian format: 12 34 56 78
Bytes in big-endian format: 12 34 56 78
This shows how the bytes of 0x12345678
are stored in memory on both little-endian and big-endian systems.