Article From:https://www.cnblogs.com/leehm/p/9967667.html

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

    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

    Small-end byte order mode:It means that the high bytes of data are stored in the high address of memory, while the low bytes of data are stored in the low address of memory. This is more in line with the habit of logical thinking, high to high, low to low.
 
     Visualize the following picture: (Photo Source Network)
     Memory: high tail: high tail, low tail: tail in position
 

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 

#include <stdio.h>

 

int main()

{

   int i = 0x12345678;

   char *c =(char *)&i;

   if(c[0] == 0x12 && c[1] == 0x34 && c[2] == 0x56 && c[3] == 0x78)

   {

       printf(“Big-Endian Mode\n”);

   }

   else

   {

       printf(“Little-Endian Mode\n”);

   }

 

   return 0;

}

ch71mlp000277:~ rtmptest$ gcc -o test test.c 

ch71mlp000277:~ rtmptest$ ./test 

Little-Endian Mode

ch71mlp000277:~ rtmptest$ 

    2. The corresponding values are matched by the common starting address feature of the consortium.

#include <stdio.h>

 

typedef union

{

   unsigned short a;

   unsigned char b[2];

}u_data;

 

int main()

{

  u_data data;

  data.a = 0x1234;

  //union data struct: a&b own the same 2 bytes address.

  //b[0] is a’s lower address

  //b[1] is a’s higher address.

  if(data.b[0] == 0x12 && data.b[1] == 0x34)

  {

     printf(“Big-Endian Mode\n”);

  }

  else

  {

     printf(“Little-Endian Mode\n”);

  }

  printf(“b[0]=0x%x,b[1]=0x%x\n”,data.b[0],data.b[1]);

 

  return 0;

}

ch71mlp000277:~ rtmptest$ gcc -o test1 test1.c 

ch71mlp000277:~ rtmptest$ ./test1

Little-Endian Mode

b[0]=0x34,b[1]=0x12

ch71mlp000277:~ rtmptest$ 

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.

Link of this Article: Big end small end

Leave a Reply

Your email address will not be published. Required fields are marked *