Big & Little Endian in Computers

"Big end" and "little end" can be traced back to Jonathan Swift's "Gulliver's Travels" in 1726, one of which talks about two countries because of eating eggs, whether to break the larger end or the smaller first One end of the dispute, and even a war broke out. In October 1981, Danny Cohen's article "On holy wars and a plea for peace" introduced the pair of words to the computing world. In this way, the so-called big-endian and little-endian, that is, big-endian and little-endian, are actually the description of the computer address from the description of the part of the egg. It can also be said that it is a computer term derived from a slang term. Anyone with a little common sense of English will know that it is difficult to guess the correct meaning of a slang word if it is only taken literally. In the computer, for the description of the address, "big" and "small" are rarely used to describe; correspondingly, "high" and "low" are used more; unfortunately, these terms are directly translated literally It becomes "big endian" and "little endian" when it comes over, and it is not very strange that people are confused.

 

"Big endian" and "little endian" indicate which end of a multibyte value is stored at the starting address of the value; little endian is stored at the starting address, that is, little endian byte order; big endian is stored at the starting address The address is in big-endian byte order;

Or:

1. Little-Endian means that the low-order bytes are placed at the low address end of the memory (that is, the starting address of the value), and the high-order bytes are placed at the high address end of the memory;

2. Big-Endian means that the high-order bytes are placed at the low address end of the memory (that is, the starting address of the value), and the low-order bytes are placed at the high address end of the memory;

For example, the representation of the number 0x12 34 56 78 in memory is:

1) Big endian mode:

Low address -----------------> High address

0x12  |  0x34  |  0x56  |  0x78

2) Little endian mode:

Low address-------------------> High address

0x78  |  0x56  |  0x34  |  0x12

It can be seen that the storage mode of big endian mode is similar to that of strings.

 

 

 

Why are there big and small endian modes?

This is because in computer systems, we use bytes as a unit, each address unit corresponds to a byte, and a byte is 8 bits. But in C language, in addition to 8-bit char, there are 16-bit short type and 32-bit long type (depending on the specific compiler). In addition, for processors with more than 8 bits, such as 16-bit or 32-bit , since the register width is greater than one byte, there must be a problem of arranging multiple bytes. This leads to the big endian storage mode and the little endian storage mode. For example, a 16-bit short type x, the address in memory is 0x0010, the value of x is 0x1122, then 0x11 is the high byte, and 0x22 is the low byte. For big-endian mode, 0x11 is placed in the low address, that is, 0x0010, and 0x22 is placed in the high address, that is, 0x0011. Little-endian mode, just the opposite. Our commonly used X86 structure is in little endian mode, while KEIL C51 is in big endian mode. Many ARMs and DSPs are in little-endian mode. Some ARM processors can also be selected by hardware to be big-endian or little-endian.

 

 

network byte order

The data transmitted on the network are all byte streams. For a multi-byte value, which byte should be transmitted first during network transmission? That is to say, when the receiver receives the first byte, it will Whether this byte is treated as a high-order byte or a low-order byte is a more meaningful question;

The UDP/TCP/IP protocol stipulates: treat the first byte received as the high-order byte, which requires the first byte sent by the sender to be the high-order byte; and when the sender sends data, send The first byte of the value is the byte corresponding to the starting address of the value in the memory, that is, the byte corresponding to the starting address of the value in the memory is the first high-order bit to be sent Byte (ie: the high-order byte is stored at the low address); it can be seen that before the multi-byte value is sent, it should be stored in the big endian method in the memory;

So, the network byte order is big endian;

 

 

How to test whether your compiler is big-endian or little-endian: The following code can be used to test whether your compiler is big-endian or little-endian:

#include<stdio.h>

int main(){

    short int x;

    char x0,x1;

    x=0x1122;

    x0=((char *)&x)[0]; //Low address unit

    x1=((char *)&x)[1]; //High address unit

    printf("x0=0x%x,x1=0x%x",x0,x1);// If x0=0x11, it is big endian; if x0=0x22, it is little endian...

    return 0;

}

 

Current status of big and small

At present, Intel's 80x86 series chips are the only chips that still insist on using little endian. ARM chips use little endian by default, but can be switched to big endian; while chips such as MIPS either use all big endian storage, or provide options to support big endian—— Can switch between big and small endianness. In addition, the processing of big endian is also related to the implementation of the compiler. In C language, the default is little endian (but in some implementations of single-chip microcomputers, it is based on big endian, such as Keil 51C), Java is platform-independent, the default is is big endian. Big endian is generally used to transmit data on the network.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326257588&siteId=291194637