为什么endianess在一个字节内的位之间很重要?

以下是linux机器上库的IP结构

struct ip { #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ip_hl:4; /* header length */ unsigned int ip_v:4; /* version */ #endif #if __BYTE_ORDER == __BIG_ENDIAN unsigned int ip_v:4; /* version */ unsigned int ip_hl:4; /* header length */ #endif u_int8_t ip_tos; /* type of service */ u_short ip_len; /* total length */ u_short ip_id; /* identification */ u_short ip_off; /* fragment offset field */ #define IP_RF 0x8000 /* reserved fragment flag */ #define IP_DF 0x4000 /* dont fragment flag */ #define IP_MF 0x2000 /* more fragments flag */ #define IP_OFFMASK 0x1fff /* mask for fragmenting bits */ u_int8_t ip_ttl; /* time to live */ u_int8_t ip_p; /* protocol */ u_short ip_sum; /* checksum */ struct in_addr ip_src, ip_dst; /* source and dest address */ }; 

对于这些线:

  #if __BYTE_ORDER == __LITTLE_ENDIAN unsigned int ip_hl:4; /* header length */ unsigned int ip_v:4; /* version */ #endif #if __BYTE_ORDER == __BIG_ENDIAN unsigned int ip_v:4; /* version */ unsigned int ip_hl:4; /* header length */ #endif 

为什么endianess在一个字节内很重要? 我认为endianess只影响多字节整数,但在我看来,endianess也会影响字节内的位排列?

此外,它只是一个字节,为什么它是unsigned int ,这是4个字节。

我在wireshark中注意到,ip_v和ip_hl显示为0x45如果我捕获一个IP数据包。 第一个字节由ip_v和ip_hl组成,我把它放入一个字符变量x

然后x & 0b11110000的结果是什么? 总是4没有什么东西,或者它可能是5?

endianess定义了最高有效位(MSB)的位置,它与变量内部的数字在内存中的解释方式有关。 考虑无符号整数:

 00000001 (Binary) = 1 (2 to the power of 0) -> If the most significant bit is to the left 00000001 (Binary) = 128 (2 to the power of 7) -> If the most significant bit is to the right 

正如您所看到的,最高位的位置对于内存中的数字表示非常重要,即使是8位数也是如此。

对于你的上一个问题,你是对的,如果它是1字节或4则没有区别,因为它只占用4位。 但是请记住,unsigned int并不总是一个4字节的数字。

希望能帮助到你!

存在与多字节数据相关的字节排序。 但在你的情况下,重要的是bit field ordering ,它处理单字节数据的位顺序。 C标准没有提出有关位域排序的规则。 它取决于实现,由编译器决定。

变量的大小不是4个字节。 它只有4位。 它们不是自变量。 它们是结构中的位字段。