Skip to content

Commit 4e9b021

Browse files
committed
clean up
1 parent 92d4386 commit 4e9b021

8 files changed

+831
-4
lines changed

README.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ This change ensures that VSCode uses the "Ninja Multi-Config" generator by defau
142142
- [Parallelization with std::packaged_task](docs/asynchronous_programming.md#parallelization-with-std--packaged-task)
143143
* [Attribute specifier sequence [[ attribute-list ]] ](docs/attribute_specifier_sequence.md)
144144
* [Basic IO Operation, Streams, Reading/Writing Files, Formatting Output, cin, scanf, gets, getline, printf](docs/basic_IO_operation.md)
145+
* [Big-endian,_Little-endian](docs/big-endian_little-endian.md)
145146
* [Bitset, Bit field, Bitwise Operations](docs/bitset_bit_field_bitwise_operations.md)
146147
* [Callable Objects, Callbacks](docs/callable_callbacks.md)
147148
- [1. Function Pointers](docs/callable_callbacks.md#1-function-pointers)
@@ -165,7 +166,7 @@ This change ensures that VSCode uses the "Ninja Multi-Config" generator by defau
165166
* [Const, Constexpr and Mutable](docs/const_constexpr_mutable.md)
166167
* [Immutable Objects](docs/immutable_objects.md)
167168
* [Data Types, Numerical Limits, Machine Epsilon, Precision](docs/primitive_data_types_numerical_limits_machine_epsilon_precision.md)
168-
* [Data Types Conversions, Casting](docs/type_conversions_casting.md)
169+
* [Data Types Conversions, Casting, Type Coercion](docs/type_conversions_casting_type_coercion.md)
169170
* [Decay](docs/decay.md)
170171
* [Dynamic Memory Allocation in C](docs/dynamic_memory_allocation.md)
171172
* [Enum](docs/enum.md)
@@ -299,14 +300,15 @@ This change ensures that VSCode uses the "Ninja Multi-Config" generator by defau
299300
* [C++ Translation Units](docs/translation_units.md)
300301
* [Undefined behavior, Unspecified and Implementation-Defined](docs/undefined_unspecified_implementation_defined.md)
301302
* [Printing List of All Included Headers](docs/print_all_included_headers.md)
302-
* [fPIE (Position Independent Executable) and fPIC(Position Independent Code and) ](docs/fPIE_and_fPIC.md)
303+
* [fPIE (Position Independent Executable) and fPIC(Position Independent Code and) ](docs/fPIE_and_fPIC.md)
304+
* [Lexical Analyzer](docs/)
303305

304306

305307
## [Optimizing C++](src/optimizing_cpp)
306308
## [Data File Storage Parsing](#)
307-
* [CSV ](src/third_party_tools/csv)
309+
* [CSV ](docs/csv.md)
308310
* [YAML ](src/third_party_tools/yaml)
309-
* [JASON ](src/third_party_tools/jason)
311+
* [JASON](docs/json.md)
310312
* [XML ](src/third_party_tools/xml/)
311313
## [Code Benchmarking](src/third_party_tools/benchmark)
312314
* [Google Benchmark ](src/third_party_tools/benchmark)

docs/big-endian_little-endian.md

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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

Comments
 (0)