字节序、比特序(一)

本文介绍了字节序的概念,包括大端序和小端序,以及它们在多字节数据存储中的作用。同时,讨论了比特序,指出它通常与字节序一致,并通过例子解释了位域在网络编程中的影响,特别是在处理大端和小端问题时的注意事项。此外,文章还提到了以太网传输中的特殊情况,即以太网采用大端字节序但小端比特序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


1. 字节序

    字节序即字节的存储顺序,如果数据都是单字节的,那怎么存储无所谓了,但是对于多字节数据,比如int,double等,就要考虑存储的顺序了。字节序是硬件层面的东西,通常只和你使用的处理器架构有关,而和编程语言无关。字节序分为大端序和小端序。

   大端序:数据的高位字节存放在地址的低端 低位字节存放在地址高端。

  小端序:数据的高位字节存放在地址的高端 低位字节存放在地址低端。

下面两张图分别表示了数字0x12345678在大端机和小端机中的存储。


技术分享


图1 大端序


技术分享

图2 小端序


2. 比特序

       字节序是一个对象中的多个字节之间的顺序问题,比特序就是一个字节中的8个比特位(bit)之间的顺序问题。一般情况下系统的比特序和字节序是保持一致的。二进制数值10110101为例,其在不同平台下的内存位序如下:

 大端的含义是数值的最高位1(最左边的1)放在了内存起始位置上,即数值10110101的大端内存布局为10110101。

    小端的含义是数值的最低位1(最右边的1)放在了内存起始位置上,即数值10110101的小端内存布局为10101101。

3.位域

    对于位域有一个约定:在C语言的结构体中如果包含了位域,如果位域A定义在位域B之前,那么位域A总是出现在低地址的比特位。 这就决定了网络编程中位域在定义时必须处理大小端问题。(同样,结构体中前面的成员也处于较低的地址)

        对于IP头,可以这样理解,网络传输时version在前,ihl在后,网络是大端序,可以认为version是数字的高位,ihl是低位,所以:

       在大端机中,由于低地址是高位,所以位域version必须在前面;

      在小端机中,由于高地址是高位,所以位域version必须在后面。

   

  struct iphdr {
  #if defined(__LITTLE_ENDIAN_BITFIELD)
          __u8    ihl:4,
                  version:4;
  #elif defined (__BIG_ENDIAN_BITFIELD)
          __u8    version:4,
                  ihl:4;
  #else
  #error  "Please fix <asm/byteorder.h>"
  #endif
          __u8    tos;
          __be16  tot_len;
          __be16  id;
          __be16  frag_off;
          __u8    ttl;
         __u8    protocol;
         __sum16 check;
         __be32  saddr;
         __be32  daddr;
         /*The options start here. */
 };

Transmitting the Frame (byte order and bit order)

Ethernet transmission is strange, in that the byte order is big-endian (leftmost byte is sent first), but bit order little-endian (rigthmost, or LSB (Least Significant Bit) of the byte is sent first).  For example, if you are sending a frame:

<---  

We assume the preamble has been sent, then the next to be sent is the destination MAC address - 6 bytes, but for this example we will show the first 4 bytes:

     11100001    00001111    10101010    10010011   
         Byte1            Byte2          Byte3            Byte4

The data normally moves to the left, using traditional images of frames.  This is true for each of the 4 bytes - they will be transmitted in big-endian, meaning "left byte first".  But we must reverse the bits in each byte, to show the actual serial stream of bits moving to the left.  So the actual bits being transmitted to the left, are as follows with the bits in each byte reversed:

<   10000111    11110000    01010101    11001001
         Byte1            Byte2          Byte3            Byte4
      (reversed)     (reversed)   (reversed)      (reversed)

NOTE:  Ethernet does not group bytes.  It has no idea that it is sending a 6-byte address, or a 2-byte Type, etc.  It simply sends the entire frame, one byte at a time, from left to right, with each byte being sent LSB first and MSB last.

 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值