Use C language program to determine the size of the end pattern
- 2020-04-01 21:36:28
- OfStack
1. In big-end mode, the low value of data is stored in the high address of memory, while the high value of data is stored in the low address of memory; The small-end pattern is reversed
2. Why are there different ends?
Because in a computer system, storage is in bytes, each address cell corresponds to a byte, a byte =8bit. In C, in addition to 8bit char, there are 16bit short and 32bit long (depending on the compiler). For processors with bits greater than 8 bits, such as 16-bit or 32-bit processors, how to arrange the storage of multiple bytes because the register width is greater than one byte
3. Respective advantages:
Small - end mode: cast data does not need to adjust the byte content, 1, 2, 4 bytes stored in the same way.
Big-end mode: the symbol bit is fixed as the first byte, easy to determine the positive and negative.
4. The commonly used X86 structure is the small-end mode, while KEIL C51 is the big-end mode. A lot of ARM, DSP is small - end mode.
5.C language determines the size of the end pattern
Method one:
void IsBigEndian()
{
short int a = 0x1122;//Hexadecimal, one number has four digits
char b = *(char *)&a; //By casting short(2 bytes) into a char single byte, b points to the starting byte of a (low byte)
if( b == 0x11)//Low byte save is the high byte of data
{
//Big end mode
}
else
{
//Small end mode
}
}
Method 2:
void IsBigEndian()//How it works: the storage order for a union is that all members start at a low address, and all members share storage space
{
union temp
{
short int a;
char b;
}temp;
temp.a = 0x1234;
if( temp.b == 0x12 )//Low byte save is the high byte of data
{
//Big end mode
}
else
{
//Small end mode
}
}
On my machine, I verified the small-end mode
< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201304/1364536453_5435.png ">