C program for judging the size and order
Concept
Large-end format: the high bytes of word data are stored in the low address, while the low bytes of word data are stored in the high address.
Small-end format: In contrast to the large-end storage format, the low-end storage format stores the low bytes of word data, and the high-end storage stores the high bytes of word data.
For example5A6C
Medium5A
Is high byte,6C
Is a low byte. In a large-order machine, the high byte is located in the low address, that is5A6C
Storage, according6C5A
Storage.
Why does this mode exist?
In computer systems, we use bytes. Each address unit corresponds to one byte.8bit
. HoweverC
Besides8bit
Ofchar
In addition16bit
Ofshort
Type,32bit
Oflong
Type (depending on the specific compiler ).8
Bit processors, such16
Bit or32
Because the register width is greater than one byte, there must be a problem if multiple bytes are arranged. Therefore, the large-end storage mode and the Small-end storage mode are created.
Code Implementation
Please write oneC
Function. If the processor isBig_endian
Is returned.0
IfLittle_endian
Is returned.1
int checkCPU(){ union w { int a; char b; } c; c.a = 1; return(c.b == 1);}
Analysis:
Consortiumunion
The storage order isAll members are stored from low addresses..
Short int x; char x0, x1; x = 0x1122; x0 = (char *) & x) [0]; // low address unit x1 = (char *) & x) [1]; // high address unit x 0 = 0x11 indicates a large end; if x0 = 0x22, it is a small client ......