为什么endianess在一个字节内的位之间很重要?(why endianess matters among bits inside a byte?)

编程入门 行业动态 更新时间:2024-10-28 05:18:11
为什么endianess在一个字节内的位之间很重要?(why endianess matters among bits inside a byte?)

以下是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?

the following is the IP structure from library on a linux machine

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 */ };

for these lines:

#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

why endianess matters inside a byte? I think endianess only affect multi-byte integers, but here it seems to me that endianess also affect bits arrangement inside a byte?

besides, it is only a byte, why it is unsigned int, which is 4 bytes.

I notice in wireshark, ip_v and ip_hl is shown as 0x45 If I capture an IP packet. The first byte is composed of ip_v and ip_hl I put it into a character variable x

then what is the result of x & 0b11110000 ? is it always 4 no matther what endianess, or it may be 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字节的数字。

希望能帮助到你!

The endianess defines the place of the Most Significant Bit (MSB), it is related to the way the number inside a variable is interpreted in memory. Considering unsigned integers:

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

As you can see the position of the most significant bit is very important when it comes to the number representation in memory, even in 8 bit numbers.

For your last question you are right, it makes no difference if it is 1 byte or 4 because it is taking only 4 bits. But but remember that an unsigned int not always is a 4 bytes number.

Hope it helps!

更多推荐

本文发布于:2023-04-27 21:48:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1328624.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:很重要   字节   endianess   byte   bits

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!