What is a big-endian and what is a small end:
The so-called big- endian mode is that the low data is kept in the high address of the memory, and the data is kept in the low address of memory;
The so-called small-end mode , refers to the low-level data stored in the memory of the lower address, and the high data is stored in the memory of the higher address.
Why there are size ends:
Why do you have the size and end mode of the points? This is because in the computer storage system, we are managed in bytes, each memory address cell corresponds to a byte, one byte 8bit . But in C addition to the language, the type 8bit char 16bit short , 32bit the long type (to see the specific compiler), in addition, for the number of bits greater than 8 bits of the processor, such as 16-bit or 32-bit processor, because If the register width is greater than one byte , there must be an issue where multiple bytes are scheduled . The result is a big-endian storage mode and a small-end storage mode. For example, the type of a, 16bit short x in memory address is 0x0010 , x the value is 0x1122 , then the 0x11 high byte, 0x22 is low byte. For big-endian mode, it will be placed in the 0x11 low address, that is, in the 0x0010 0x22 high address, that is, 0x0011 in. Small-end mode, just the opposite. Our common X86 structure is the small-end mode, and then the big- KEIL C51 endian mode. A lot of ARM , DSP all for the small end mode. Some ARM processors can also be selected by hardware for either big-endian or small-end mode.
Example of how the size ends are stored in memory:
For example, the 16bit wide number of 0x1234 storage modes in the Little-endian mode CPU memory (assuming that the store starts from the address 0x4000 ) is:
| memory Address |
0x4000 |
0x4001 |
| Store content |
0x34 |
0x12 |
The Big-endian mode CPU memory is stored in the following way:
| memory Address |
0x4000 |
0x4001 |
| Store content |
0x12 |
0x34 |
How to test if the compiler is a big or small end:
The following code can be used to test whether your compiler is in big-endian mode or small-end mode:
#include<stdio.h>int main(){ shortint x; char x0,x1; x=0x1122; x0=((char *)&x)[0]; //低地址单元 x1=((char *)&x)[1]; //高地址单元 printf("x0=0x%x,x1=0x%x",x0,x1);// 若x0=0x11,则是大端; 若x0=0x22,则是小端...... return0;}
Big-endian mode and small-end mode in storage