CPUs of different architectures, the order in which data is stored in memory is not the same.
The storage of data in memory is in bytes (byte) as the basic unit, so word (word) and half word (Half-word) in memory have two kinds of order, respectively called: Big Endian and small end mode (Little Endian).
The big-endian storage mode is the highest byte (most significant BIT,MSB) of the word or half word is stored in the lowest bit byte address of memory, while the low byte of the word data is stored in the high address. For example, there is a word for 0x12345678, the word consists of 4 bytes, from high to low order: 0x12,0x34,0x56,0x78. If the word is stored in memory in the beginning of 0x00000000, the word is actually stored in memory in the following table:
Memory address |
Stored data (Byte) |
0x00000000 |
0x12 |
0x00000001 |
0x34 |
0x00000002 |
0x56 |
0x00000003 |
0x78 |
0x00000004 |
...... |
The order of the big-endian pattern is like the order we usually write, write the large number first, then write the decimal. In addition, the large-endian storage sequence is also widely used in TCP/IP protocol, so called network byte order .
The small-end storage mode is the lowest bit byte (Lowest significant bit,lsb) of the word or half word is stored in the lowest bit byte address of memory, while the high byte of the word data is stored in the high address. Also take 0x12345678 as an example, the following table shows the storage in small-end mode:
Memory address |
Stored data (Byte) |
0x00000000 |
0x78 |
0x00000001 |
0x56 |
0x00000002 |
0x34 |
0x00000003 |
0x12 |
0x00000004 |
...... |
The points to note are:
(1) The data in the register is in the big-endian mode in the order of storage.
(2) for in-memory data stored in small-ended mode. When CPU access is counted, the transition between the small and the big ends is hardware-based, without the overhead of data loading/storing.
Know the concept of the size-end mode, but if we write the code in C to determine whether a CPU is big-endian or small-end mode should be how to do it.
To use the simple implementation of C is to use the Union (Union), the simple Union is a structure, in the union all data members share a storage space, at the same time can only store one of the data members, all data members with the same starting address, The offset is 0 relative to the base address;
Using Union to judge, the specific code is as follows:
int Checkendian (void)
{
Union check
{
int Word;
char half;
} Endian;
endian.word=1;
if (1 = = endian.half)
return Little_endian;
else
return Big_endian;
}
http://blog.csdn.net/czzhuzc/article/details/7629548