|
| 1 | +## Big-endian, Little-endian |
| 2 | +"big-endian" and "little-endian" are terms that describe the order in which bytes are arranged within a binary representation of a number. |
| 3 | + |
| 4 | +- **Big-endian**: The most significant byte (the "big end") is stored at the smallest memory address. |
| 5 | +- **Little-endian**: The least significant byte (the "little end") is stored at the smallest memory address. |
| 6 | + |
| 7 | +Here's a simple C++ example to illustrate big-endian byte order: |
| 8 | + |
| 9 | +1. **Understand the concept**: Let's consider the integer `0x12345678`. In big-endian order, this integer is stored in memory as: |
| 10 | + |
| 11 | + ``` |
| 12 | + Address Value |
| 13 | + 0x00 12 |
| 14 | + 0x01 34 |
| 15 | + 0x02 56 |
| 16 | + 0x03 78 |
| 17 | + ``` |
| 18 | + |
| 19 | +2. **Code Example**: |
| 20 | + 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. |
| 21 | + |
| 22 | +```cpp |
| 23 | +#include <iostream> |
| 24 | + |
| 25 | +// Function to check if the system is big-endian |
| 26 | +bool isBigEndian() { |
| 27 | + unsigned int test = 1; |
| 28 | + char *byte = (char*)&test; |
| 29 | + return byte[0] == 0; |
| 30 | +} |
| 31 | + |
| 32 | +// Function to print bytes of an integer |
| 33 | +void printBytes(int value) { |
| 34 | + unsigned char *bytes = (unsigned char*)&value; |
| 35 | + for (size_t i = 0; i < sizeof(int); ++i) { |
| 36 | + std::cout << std::hex << (int)bytes[i] << " "; |
| 37 | + } |
| 38 | + std::cout << std::dec << std::endl; |
| 39 | +} |
| 40 | + |
| 41 | +int main() { |
| 42 | + int number = 0x12345678; |
| 43 | + |
| 44 | + std::cout << "System is " << (isBigEndian() ? "Big-endian" : "Little-endian") << std::endl; |
| 45 | + |
| 46 | + std::cout << "Original integer: 0x" << std::hex << number << std::dec << std::endl; |
| 47 | + |
| 48 | + std::cout << "Bytes in memory: "; |
| 49 | + printBytes(number); |
| 50 | + |
| 51 | + return 0; |
| 52 | +} |
| 53 | +``` |
| 54 | +
|
| 55 | +### Explanation: |
| 56 | +
|
| 57 | +1. **isBigEndian Function**: |
| 58 | + - We create an unsigned integer `test` with a value of `1`. |
| 59 | + - We then cast the address of `test` to a `char*` (pointer to a byte). |
| 60 | + - If the first byte (`byte[0]`) is `0`, it means the most significant byte is at the lowest address, indicating a big-endian system. |
| 61 | +
|
| 62 | +2. **printBytes Function**: |
| 63 | + - This function prints out the bytes of an integer. |
| 64 | + - We cast the address of the integer to an `unsigned char*` to access individual bytes. |
| 65 | + - We then print each byte in hexadecimal format. |
| 66 | +
|
| 67 | +3. **main Function**: |
| 68 | + - We define an integer `number` with the value `0x12345678`. |
| 69 | + - We check and print whether the system is big-endian or little-endian. |
| 70 | + - We print the original integer in hexadecimal format. |
| 71 | + - We call `printBytes` to print the bytes of the integer as they are stored in memory. |
| 72 | +
|
| 73 | +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. |
| 74 | +
|
| 75 | +
|
| 76 | +
|
| 77 | +
|
| 78 | +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. |
| 79 | +
|
| 80 | +Here’s how `0x12345678` would be stored in memory on a little-endian system: |
| 81 | +
|
| 82 | +``` |
| 83 | +Address Value |
| 84 | +0x00 78 |
| 85 | +0x01 56 |
| 86 | +0x02 34 |
| 87 | +0x03 12 |
| 88 | +``` |
| 89 | +
|
| 90 | +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: |
| 91 | +
|
| 92 | +```cpp |
| 93 | +#include <iostream> |
| 94 | +
|
| 95 | +// Function to check if the system is big-endian |
| 96 | +bool isBigEndian() { |
| 97 | + unsigned int test = 1; |
| 98 | + char *byte = (char*)&test; |
| 99 | + return byte[0] == 0; |
| 100 | +} |
| 101 | +
|
| 102 | +// Function to print bytes of an integer in the current system's endian format |
| 103 | +void printSystemBytes(int value) { |
| 104 | + unsigned char *bytes = (unsigned char*)&value; |
| 105 | + for (size_t i = 0; i < sizeof(int); ++i) { |
| 106 | + std::cout << std::hex << (int)bytes[i] << " "; |
| 107 | + } |
| 108 | + std::cout << std::dec << std::endl; |
| 109 | +} |
| 110 | +
|
| 111 | +// Function to print bytes of an integer in big-endian format |
| 112 | +void printBigEndianBytes(int value) { |
| 113 | + unsigned char *bytes = (unsigned char*)&value; |
| 114 | + for (int i = sizeof(int) - 1; i >= 0; --i) { |
| 115 | + std::cout << std::hex << (int)bytes[i] << " "; |
| 116 | + } |
| 117 | + std::cout << std::dec << std::endl; |
| 118 | +} |
| 119 | +
|
| 120 | +int main() { |
| 121 | + int number = 0x12345678; |
| 122 | + |
| 123 | + std::cout << "System is " << (isBigEndian() ? "Big-endian" : "Little-endian") << std::endl; |
| 124 | + |
| 125 | + std::cout << "Original integer: 0x" << std::hex << number << std::dec << std::endl; |
| 126 | + |
| 127 | + std::cout << "Bytes in system's endian format: "; |
| 128 | + printSystemBytes(number); |
| 129 | + |
| 130 | + std::cout << "Bytes in big-endian format: "; |
| 131 | + printBigEndianBytes(number); |
| 132 | + |
| 133 | + return 0; |
| 134 | +} |
| 135 | +``` |
| 136 | + |
| 137 | +### Explanation: |
| 138 | + |
| 139 | +1. **printSystemBytes Function**: |
| 140 | + - This function prints out the bytes of an integer as they are stored in the current system's endian format. |
| 141 | + - It accesses the bytes by casting the address of the integer to an `unsigned char*` and prints each byte in hexadecimal format. |
| 142 | + |
| 143 | +2. **printBigEndianBytes Function**: |
| 144 | + - This function prints the bytes of the integer in big-endian format, regardless of the system's actual byte order. |
| 145 | + - It iterates through the bytes in reverse order to simulate big-endian storage. |
| 146 | + |
| 147 | +3. **main Function**: |
| 148 | + - Defines an integer `number` with the value `0x12345678`. |
| 149 | + - Checks and prints whether the system is big-endian or little-endian. |
| 150 | + - Prints the original integer in hexadecimal format. |
| 151 | + - Calls `printSystemBytes` to print the bytes in the system's endian format. |
| 152 | + - Calls `printBigEndianBytes` to print the bytes in big-endian format. |
| 153 | + |
| 154 | +### Output Example: |
| 155 | +On a little-endian system, the output might look like this: |
| 156 | +``` |
| 157 | +System is Little-endian |
| 158 | +Original integer: 0x12345678 |
| 159 | +Bytes in system's endian format: 78 56 34 12 |
| 160 | +Bytes in big-endian format: 12 34 56 78 |
| 161 | +``` |
| 162 | + |
| 163 | +On a big-endian system, the output might look like this: |
| 164 | +``` |
| 165 | +System is Big-endian |
| 166 | +Original integer: 0x12345678 |
| 167 | +Bytes in system's endian format: 12 34 56 78 |
| 168 | +Bytes in big-endian format: 12 34 56 78 |
| 169 | +``` |
| 170 | + |
| 171 | +This shows how the bytes of `0x12345678` are stored in memory on both little-endian and big-endian systems. |
0 commit comments