Big Endian & Little Endian & Network Order & Host Order & Bit Order & Bit Field

Little endian: Store the low-order byte at the starting address 
Big endian: Store the high-order byte at the starting address 

Example: The memory address of the storage method of double word 0x01020304 (DWORD) in the 
memory (Note: the memory address is from low to high from left to right)
    4000 4001 4002 4003 
LE 04 03 02 01 --- In line with human thinking, low value Stored in low address, high value stored in high address
BE 01 02 03 04 --- Intuitive, the order of reading the value is the same as the order of the address, or the order of filling in the memory is the same as that of reading


The endianness of the currently running machine can be displayed directly through the following applet:

short  int  x;
char  x0;
x=0x1122;
x0=(( char *)&x)[0];
If x0=0x11, it is big endian; if x0=0x22, it is little endian...


The network byte order is big endian. In fact, big endian or network order, we can treat it as a string of strings. For example 192.168.106.3

The execution result of the following program on the x86 platform is:

printf(“0x%x”, inet_addr("192.168.106.3"));

printf(“0x%x”, ntohl(inet_addr("192.168.106.3")));

printf(“0x%x”, htonl(inet_addr("192.168.106.3")));


0x36aa8c0

0xc0a86a3

0xc0a86a3


In fact, "192.168.106.3" is a string. On the x86 platform (little endian), the low address of the string (low on the left) is stored in the low position, so 192, that is, c0, is stored in the low position, that is, in memory The order of saving is

Memory: Low----->High

Saved value: c0 a8 6a 3

Converted to int type, that is, 0x36aa8c0;

And ntohl will reverse the entire order (on the little-endian host), and the result is 0xc0a86a3; at the same time, we see that the htonl implementation is actually the same as ntohl .


Functions such as ntohs are functions for converting host order and network order:
in systems using little endian, these functions will convert the byte order. 
In systems using big endian types, these functions will be defined as empty macros 


Different operating systems run on different CPUs, and the byte order is also different, see the table below. 
Processor OS Byte Ordering 
Alpha All Little endian 
HP-PA NT Little endian 
HP-PA UNIX Big endian 
Intelx86 All Little endian <-----x86 systems are little endian systems 
Motorola680x() All Big endian 
MIPS NT Little endian 
MIPS UNIX Big endian 
PowerPC NT Little endian 
PowerPC non-NT Big endian <-----PPC system is big endian system 
RS/6000 UNIX Big endian 
SPARC UNIX Big endian 
IXP1200 ARM core all Little endian


When defining protocol packets, when it comes to bit fields, it is also necessary to distinguish between big and small ends. For example, if the ip header version is high and ihl is low, in the case of little endian (the low value is in the low address of the address), the version is defined at the back, and the big end is Quite the opposite.

  1. struct iphdr 
  2.   { 
  3. #if __BYTE_ORDER == __LITTLE_ENDIAN 
  4.     unsigned int ihl:4; 
  5.     unsigned int version:4; 
  6. #elif __BYTE_ORDER == __BIG_ENDIAN 
  7.     unsigned int version:4; 
  8.     unsigned int ihl:4; 
  9. #else 
  10. # error "Please fix <bits/endian.h>" 
  11. #endif 
  12.     u_int8_t tos; 
  13.     u_int16_t tot_len; 
  14.     u_int16_t id; 
  15.     u_int16_t frag_off; 
  16.     u_int8_t ttl; 
  17.     u_int8_t protocol; 
  18.     u_int16_t check; 
  19.     u_int32_t saddr; 
  20.     u_int32_t daddr; 
  21.     /*The options start here. */ 
  22.   };





 

Guess you like

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