This program I will use two different methods to simulate the implementation, before this if you do not know what is the size of the friends can go to see my other blog, there is a detailed introduction, the link is as follows: juejin.cn/post/705741… Without further ado, let’s get started.
The first byte of a number stored in memory is the contents of the first byte of a number stored in memory. Such as defining an int type integer 1, because of the type int values stored in the memory is based on binary complement, endures memory is stored in the form of hexadecimal, so the integer 1 as hexadecimal number is 00 00 00 01 (4 2 hexadecimal number convert hexadecimal number 1), the low values of 01, high value of 00, Because the big-endian storage is the storage of the low value bits to the high address, the small-endian storage is the storage of the low value bits to the low address, if we read its first byte content is 0, it means that the high value bit 00 is stored in the low address, so it is big-endian storage. If the content of the first byte is 1, it indicates that the low value bit 01 is stored in the low address. Therefore, it is a small-endian storage. So how do you read the first byte of a number? Here we summarize two methods:
Method 1, pointer dereference:
When we looked at Pointers, we learned that there are Pointers to the type char, and char* happens to be a byte, so we can cast int* to char* and dereference char* to get the value of the first byte. The code is as follows:
#include<stdio.h>
int check_sys()
{
int i = 1;/ / 00 00 00 01
if (*((char*)&i) == 1)// Cast int* to char* and dereference it
{
return 1;/ / the small end
}
else
{
return 0;/ / big end
}
}
int main()
{
int ret=check_sys();
if (ret == 1)
{
printf("Little end \ n");// The lower digit is placed at the lower address
}
else
{
printf("The big end \ n");// The lower digit is placed at the higher address
}
return 0;
}
Copy the code
Debugging result:
Output result:
Method 2: Skillfully use the combination:
Note: If you are not familiar with the association, please check out my other blog, which has a detailed explanation. The link is as follows: juejin.cn/post/706665…
We know that members of a consortium share the same space, so we can use this property to access the contents of the first byte of a number, such as the following consortium:
union Un
{
char c;
int i;
}
Copy the code
The association of c and I will share the space of a byte, so we can take the I assignment 1, the 1 make up the space of four bytes, then take out c, if the 1 in the four bytes of space is stored in main mode, the c value should be 0, if stored in small side model, the c value should be 1.
The code is as follows:
#include<stdio.h>
int check_sys()
{
union U // create union U
{
char c;
int i;
}un;
un.i = 1; // Assign 1 to I
return un.c;
// if c is 0, return 0; If the value of c is 1, return 1
}
int main()
{
int ret=check_sys();
if (ret == 1)
{
printf("Little end \ n");// The lower digit is placed at the lower address
}
else
{
printf("The big end \ n");// The lower digit is placed at the higher address
}
return 0;
}
Copy the code
Debugging result:
Output result:
If you think this article is helpful to you, you can click a “like” or click a wave of favorites to support it oh, welcome your criticism and correction, let’s see you next topic!