编程-Byte order Bit order

news/2024/7/24 4:59:37 标签: c#

https://mp.weixin.qq.com/s/B9rKps4YsLiDTBkRks8rmQ

看到比特序和字节序放在一起被提及,想必就已经填补了概念拼图里面缺失的那一块了,这一块正是比特序。

 

一直以来,接触到最多的就是字节序:

大端字节序:big-endian byte order;

小端字节序:little-endian byte order;

网络字节序:network byte order;

 

大小端字节序转换:

/* 大端字节序 */
i = (data[3]<<0) | (data[2]<<8) | (data[1]<<16) | (data[0]<<24);
/* 小端字节序 */
i = (data[0]<<0) | (data[1]<<8) | (data[2]<<16) | (data[3]<<24);

 

网络字节序转换:

 uint32_t htonl(uint32_t hostlong); 
 uint16_t htons(uint16_t hostshort); 
 uint32_t ntohl(uint32_t netlong); 
 uint16_t ntohs(uint16_t netshort); 

是不是很熟悉?

 

见多识广的想必也见过这个(from linux kernel  include/uapi/linux/tcp.h ):

struct tcphdr {
    __be16    source;
    __be16    dest;
    __be32    seq;
    __be32    ack_seq;
#if defined(__LITTLE_ENDIAN_BITFIELD)
    __u16    res1:4,
        doff:4,
        fin:1,
        syn:1,
        rst:1,
        psh:1,
        ack:1,
        urg:1,
        ece:1,
        cwr:1;
#elif defined(__BIG_ENDIAN_BITFIELD)
    __u16    doff:4,
        res1:4,
        cwr:1,
        ece:1,
        urg:1,
        ack:1,
        psh:1,
        rst:1,
        syn:1,
        fin:1;
#else
#error    "Adjust your <asm/byteorder.h> defines"
#endif    
    __be16    window;
    __sum16    check;
    __be16    urg_ptr;
};

明显跟我们之前见到的以字节为单位进行转换的不同,这里是以bit为单位;

不深究的话,可能看到BIG_ENDIAN和LITTLE_ENDIAN还有下面的 "Adjust your <asm/byteorder.h> defines" ,就觉得是byte order的变种,也理解成byteorder就行了。

 

如果知道bit order的概念,就知道这是两个不同的概念了。

 

PS. 关于上面tcphdr的定义:

1. 条件编译的宏是 :

__LITTLE_ENDIAN_BITFIELD
__BIG_ENDIAN_BITFIELD

 意思是比特域的大小端,和字节序大小端是分开的:

__LITTLE_ENDIAN
__BIG_ENDIAN 

 

2. 下面的#error又说是byteorder,

#error    "Adjust your <asm/byteorder.h> defines"

是否矛盾?这里的情况是,linux kernel中把字节序和比特序都放在byteorder.h里面定义。这里只说调整这个头文件的定义,并没有说调整字节序的定义,所以应该没问题。

 include/uapi/linux/byteorder/big_endian.h 中的定义:

#ifndef _UAPI_LINUX_BYTEORDER_BIG_ENDIAN_H
#define _UAPI_LINUX_BYTEORDER_BIG_ENDIAN_H

#ifndef __BIG_ENDIAN
#define __BIG_ENDIAN 4321
#endif
#ifndef __BIG_ENDIAN_BITFIELD
#define __BIG_ENDIAN_BITFIELD
#endif

 

转载于:https://www.cnblogs.com/wjcdx/p/9147776.html


http://www.niftyadmin.cn/n/1557806.html

相关文章

js(Es6)面试题总结

Es6 面试 Let 有块级作用域 不存在变量声明提前 不允许重复声明 声明的全局变量不会挂在到window上 const 声明一个只读的常量。一旦声明&#xff0c;常量的值就不能改变 有块级作用域 不存在变量声明提前 不允许重复声明 声明的全局变量不会挂在到window上 解构 从数…

linux:yum和apt-get的区别

2019独角兽企业重金招聘Python工程师标准>>> 一般来说著名的linux系统基本上分两大类&#xff1a; 1.RedHat系列&#xff1a;Redhat、Centos、Fedora等 2.Debian系列&#xff1a;Debian、Ubuntu等 RedHat 系列 1 常见的安装包格式 rpm包,安装rpm包的命令是“rpm…

微信小程序 初始化进入首页时 onShow加载两次解决方法

问题说明&#xff1a; 进入首页时&#xff0c;在onShow中发起请求加载数据&#xff0c;发现方法被调用了两次&#xff1a; 解决&#xff1a; 1、data中定义初始数据 hasOnShow&#xff1a; false 2、onshow函数中&#xff1a; onShow: function () {if (this.data.hasOnShow)…

列表中的字典排序

list1 [ {age: 4, name: 李四, sex: 1}, {age: 5, name: 王五, sex: 1}, {age: 6, name: 赵六, sex: 1}, {age: 3, name: 张三, sex: 1}, {age: 3, name: 张小三, sex: 0},]print (sorted(list1, keylambda dictx: (dictx[age], dictx[sex]),reverseFalse)) pr…

js面试题(2019最新)

Js面试题 数据类型有哪些&#xff0c;那些事基本数据类型&#xff0c;哪些是引用数据类型&#xff0c;如何判断&#xff1f; Js数据类型共8种: 1.Number类型 2.String类型 3.Boolean类型 4.Undefined类型 5.Null类型 6.Object类型 7.Symbol 8.Array类型 其中Object类型…

[转] CocoaPods详解之----使用篇

http://blog.csdn.net/wzzvictory/article/details/18737437

The client-side rendered virtual DOM tree is not matching server-rendered content

最初写法&#xff1a; 报异常&#xff1a; vue.js:597 [Vue warn]: The client-side rendered virtual DOM tree is not matching server-rendered content. This is likely caused by incorrect HTML markup, for example nesting block-level elements inside <p>, or…