Home  >  Article  >  What is the difference between big endian mode and little endian mode?

What is the difference between big endian mode and little endian mode?

青灯夜游
青灯夜游Original
2020-12-07 12:04:3545652browse

Difference: In big-endian mode, the high byte of word data is stored in the low address, and the low byte of word data is stored in the high address; contrary to the big-endian storage mode, in the little-endian storage mode In the low address, the low byte of word data is stored, and the high address stores the high byte of word data.

What is the difference between big endian mode and little endian mode?

#The operating environment of this article: Windows 7 system, Dell G3 computer.

Related recommendations: "Introduction to Programming"

The difference between big endian mode and little endian mode

In addition to the 8-bit char type in C language, there are also 16-bit short type and 32-bit long type (depending on the specific compiler). For processors with more than 8 bits, such as 16-bit Or for a 32-bit processor, since the register width is larger than one byte, there must be a problem of how to arrange multiple bytes. This leads to big-endian storage mode and little-endian storage mode.

Big endian mode:

The high byte of word data is stored in the low address, while the low byte of word data is stored in the high address.

Little endian mode:

Contrary to the big endian storage mode, in the little endian storage mode, the low byte of the word data is stored in the low address, and the high byte of the word data is stored in the low address. The address stores the high byte of word data.

For example, the way the 16-bit wide number 0x1234 is stored in the little-endian mode CPU memory (assuming it is stored starting from address 0x4000) is:

Memory address

0x4000

0x4001

Storage content

0x34

0x12

And in big endian mode CPU The storage method in the memory is:

Memory address

0x4000

0x4001

Storage content

0x12

0x34

The storage method in big-endian mode CPU memory is:

Memory address

0x4000

0x4001

0x4002

0x4003

Storage content

0x12

0x34

0x56

0x78

The X86 structure we commonly use is Little endian mode, while KEIL C51 is big endian mode. Many ARM and DSP are in little-endian mode. Some ARM processors can also select big-endian or little-endian mode by hardware.

Note: Using the big-endian method to store data is in line with normal human thinking, while using the little-endian method to store data is conducive to computer processing.

(My understanding: little endian mode puts a low bit in the low byte)

The following code can be used to test whether your compiler is in big endian mode or Little endian mode:

int main()
{
short int x;
char x0,x1;
x=0x1122;
x0=*((char*)&x); //低地址单元 ,或者((char*)&x)[0];
x1=*((char*)&x + 1); //高地址单元,或者((char*)&x)[1];
printf("x0=%x\nx1=%x\n",x0,x1);
}

If x0=0x11, it is big endian; if x0=0x22, it is little endian....

Want to read more related For articles, please visit PHP中文网! !

The above is the detailed content of What is the difference between big endian mode and little endian mode?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn