Assume that the storage starts from the address 0x4000:
0x12345678 is also a 32-bit four-byte data, the maximum byte is 0x12, the lowest byte is 0x78:
In little-Endian mode, the CPU memory is stored as follows:
(High-byte, low-byte)
Memory Address |
Zero X 4000 |
Zero X 4001 |
Zero X 4002 |
Zero X 4003 |
Store content |
0x78 |
0x56 |
0x34 |
0x12 |
In big-Endian mode, the CPU memory is stored as follows:
(The high byte is at the low address, and the low byte is at the high address)
Memory Address |
Zero X 4000 |
Zero X 4001 |
Zero X 4002 |
Zero X 4003 |
Store content |
0x12 |
0x34 |
0x56 |
0x78 |
As shown in the preceding table,
The main difference between data storage in the size mode is that the data is stored in the byte order. The large-end mode stores the data in the high-end mode and the Small-end mode stores the data in the high-end mode.
The use of big-end data storage is in line with human normal thinking, while the use of small-end data storage is conducive to computer processing. So far, data has been stored on large or small ends, and its advantages and disadvantages are still unknown.
Some processor systems store data in a small way, such as Intel's Pentium. Some processor systems use big-end data storage methods, such as IBM semiconductors and Freescale PowerPC processors. In addition to the processor, some peripherals are also designed to store data on large or small ends.
Question 1: Write a c function. If the processor is big_endian, 0 is returned. If the processor is little_endian, 1 is returned.
int checkCPU( ){ { union w { int a; char b; } c; c.a = 1; return (c.b ==1); }}
Analysis:
The order in which union members are stored starts from the low address.
How does one store c. A on a small client?
Address
------------------------------------
| A + 1 | A + 2 | A + 3 | int;
| 0x01 | 0x00 | 0x00 | 0x00 |
-------------------------------------
| A | char B;
| 0x01 |
---------
How does one store c. A on a big end?
Address
------------------------------------------
| A + 1 | A + 2 | A + 3 | int;
| 0x00 | 0x00 | 0x00 | 0x01 |
------------------------------------------
| A | char B;
| 0x00 |
---------