Write a C function. If the processor is Big_endian, 0 is returned. If Little_endian is used, 1 is returned.
As we all know, the method for storing the number of CPUs in the Little-endian mode is from low bytes to high bytes, the Big-endian mode stores operands in bytes from high to low.
For example, the storage method of 16-bit 0x1234 in Little-endian mode CPU memory (assuming that it starts from address 0x4000) is:
Memory Address storage content
0X4000 0x34
0x4001 0x12
In Big-endian mode, the CPU memory is stored as follows:
Memory Address storage content
0X4000 0x12
0x4001 0x34
32-bit-width 0x12345678 storage method in the Little-endian mode CPU memory (assuming it starts from address 0x4000:
Memory Address storage content
0X4000 0x78
0x4001 0x56
0x4002 0x34
0x4003 0x12
In Big-endian mode, the CPU memory is stored as follows:
Memory Address storage content
0X4000 0x12
0x4001 0x34
0x4002 0x56
0x4003 0x78
The source code can be written as follows:
Int checkCPU ()
{
{
Union u
{
Int;
Char B;
} C;
C. a = 1;
Return (c. B = 1 );
}
}
Www.2cto.com
The storage order of union is that all members are stored from the low address. With this feature, the CPU can easily read and write the memory in the Little-endian or Big-endian mode.
This is easier and faster than any judgment statement!
Please follow Li Mu space www.limou.net