为什么字节序中字节之间的位序关系重要? [英] why endianess matters among bits inside a byte?

查看:113
本文介绍了为什么字节序中字节之间的位序关系重要?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是Linux计算机上库中的IP结构

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

这些行:

    #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?

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

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

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

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

那么x & 0b11110000的结果是什么?总是4个没有什么耐久性,或者可能是5个?

then what is the result of x & 0b11110000 ? is it always 4 no matther what endianess, or it may be 5?

推荐答案

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

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

如您所见,即使在8位数字中,最高有效位的位置对于内存中的数字表示也非常重要.

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.

对于您的最后一个问题,您是对的,它是1个字节还是4个字节都没有关系,因为它仅占用4位.但是请记住,一个无符号的int并不总是4个字节的数字.

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.

希望有帮助!

这篇关于为什么字节序中字节之间的位序关系重要?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆