Because of a problem, I suddenly think of the big-end and small-end problems recently.
Long time ago, the memory is a little vague, so look again, make a record, most of the content comes from the great Internet…
- 1. Big end small end concept
- 2. Usage scenarios
- 3. Write programs to determine the size of the end (occasionally encountered in interviews)
- 4. Why are there big and small end modes?
1. Big end small end concept
In fact, the big end and the small end are what we call them in the popular sense. In fact, they refer to the sequential pattern of bytes stored by computers, which can be divided into two types according to the way data is stored in memory.Big End Byte Order ModeandSmall End Byte Order Mode。
Large end byte order mode:It means that the high bytes of data are stored in the low address of memory, while the low bytes of data are stored in the high address of memory.In line with our reading habits。
2. Usage scenarios
Generally, the data on the network are interacted in the large-end data mode, while most of our hosts are processed in the small-end data mode, so when doing network communication, often need to be converted.
I once encountered a socket communication when the two sides of the binary structure, and then read out the data is very strange, the query found that it is a byte order problem.。
socketProgramming, the following functions should not be unfamiliar
htons()、htonl()Converting 16-bit and 32-bit host data into network byte order, also known as large-end byte order, respectively
ntohs()、ntohl()Then 16-bit and 32-bit network byte sequence are converted to host data respectively.
3. Write programs to determine the size of the end (occasionally encountered in interviews)
1. Direct forcing, matching the value of the corresponding memory address, simple and clear.
-rwxr-xr-x 1 rtmptest staff 8432 Nov 16 09:37 test
-rw-r–r– 1 rtmptest staff 267 Nov 15 16:45 test.c
ch71mlp000277:~ rtmptest$ cat test.c
int i = 0x12345678;
char *c =(char *)&i;
if(c == 0x12 && c == 0x34 && c == 0x56 && c == 0x78)
ch71mlp000277:~ rtmptest$ gcc -o test test.c
ch71mlp000277:~ rtmptest$ ./test
2. The corresponding values are matched by the common starting address feature of the consortium.
unsigned short a;
unsigned char b;
data.a = 0x1234;
//union data struct: a&b own the same 2 bytes address.
//b is a’s lower address
//b is a’s higher address.
if(data.b == 0x12 && data.b == 0x34)
ch71mlp000277:~ rtmptest$ gcc -o test1 test1.c
ch71mlp000277:~ rtmptest$ ./test1
4. Why are there big and small end modes?
Contents from: https://bbs.csdn.net/topics/390968161
This is because in computer systems, we are in bytes, each address unit corresponds to a byte, a byte is 8 bits.
But in C language, besides 8 bit char, there are 16 bit short type and 32 bit long type (depending on the specific compiler).
In addition,For processors with bits larger than 8, such as 16-bit or 32-bit processors, because the width of registers is larger than one byte, there must be a problem of how to arrange multiple bytes.。
This leads to large-end storage mode and small-end storage mode.
For example, a short type X of 16 bits, whose address in memory is 0x0010 and the value of X is 0x1122, then 0X11 is a high byte and 0x22 is a low byte. For the large-end mode, put 0X11 in the low address, that is, 0x0010, and 0x22 in the low address.In the high address, that is, 0x0011. Small-end mode, just the opposite. The commonly used X86 structure is the small-end mode, while KEIL C51 is the large-end mode. Many ARM and DSP are small-end modes. Some ARM processors can also choose from hardware to be large-end mode or small-end mode.Type.