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 example5A6CMedium5AIs high byte,6CIs a low byte. In a large-order machine, the high byte is located in the low address, that is5A6CStorage, according6C5AStorage.
Why does this mode exist?
In computer systems, we use bytes. Each address unit corresponds to one byte.8bit. HoweverCBesides8bitOfcharIn addition16bitOfshortType,32bitOflongType (depending on the specific compiler ).8Bit processors, such16Bit or32Because 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 oneCFunction. If the processor isBig_endianIs returned.0IfLittle_endianIs returned.1
int checkCPU(){ union w { int a; char b; } c; c.a = 1; return(c.b == 1);}
Analysis:
ConsortiumunionThe 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 ......