This is the 29th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021.”
Recently, I want to review C language, so I will update an article about C language in nuggets every day! Freshmen who are just learning C, and those who want to review C /C++, don’t miss it! Solid foundation, slow down is fast!
Lead out big and small end
We can see that we store the complement for a and b, respectively. But we found something wrong with the order. Why and why?
What is the big and small end
In big-endian (storage) mode, the low value of index data is stored in the high address of memory, while the high value of data is stored in the low address of memory.
In the little endian (storage) mode, the low value of data is stored in the low address of memory, while the high value of data is stored in the high address of memory
Why do we have big and small ends
Why are there big and small modes? This is because in a computer system, we are measured in bytes, one byte for each address unit, and one byte is 8 bits. But in C, in addition to the 8-bit char, there are 16-bit short, 32-bit long (depending on the compiler), and for processors with more than 8 bits, such as 16-bit or 32-bit processors, since the register width is larger than one byte, Then there must be a problem of how to arrange multiple bytes. This leads to big-endian storage and small-endian storage.
Baidu Pen test
Baidu system Engineer in 2015
Please briefly describe the concepts of big-endian and small-endian, and design a small program to determine the current machine’s byte order. (10)
Analysis of the
Just access the first byte, and if the value is 1, it’s the little endian, and if the value is 0, it’s the big endian
You just have to look at whether the first byte is 0 or 1
-> Take only one byte -> strong to char*
code
Notation 1: hard core dry
int main() { int a = 1; char* p (char*)&a; If (*p == 1) {printf(" little end \n"); } else {printf(" big end \n"); } return 0; }Copy the code
Notation 2: Write it as a function
Returns 1 if it’s the small end and 0 if it’s the big end
Int check_sys() {int a = 1; char* p = (char*)&a; if(*p == 1) { return 1; } else { return 0; } } int main() { int ret = check_sys(); If (ret == 1) {printf(" little end \n"); } else {printf(" big end \n"); } return 0; }Copy the code
So let’s write 3 minus optimize
0s int check_sys() {int a = 1; char*p = (char*)&a; return *p; //p is a char, dereferencing a byte}Copy the code
Notation 4- optimization
0s int check_sys() {int a = 1; return *(char*)&a; }Copy the code
Method 2: Use unions
The members of the union share the same memory space and are stored from the starting position, so that the size of a union variable is at least the size of the largest member (because the union must be able to hold at least the largest member).
union un
{
char c;
int a;
};
int check_sys()
{
union un u;
u.a = 1;
return u.c;
}
Copy the code
That’s all for today. Thank you for seeing us! Hope to help you! You are welcome to click on this topic and subscribe! At the same time, welcome the bigwigs to criticize and correct!