当前位置:网站首页>[Structural Internal Power Cultivation] Structural Realization Stages (2)
[Structural Internal Power Cultivation] Structural Realization Stages (2)
2022-08-05 07:56:00 【Albert Edison】
前言
上一篇文章讲解了 结构体内存对齐,This article talks about the ability of structs to implement bit segments
1. 什么是位段
位段的声明和结构是类似的,有两个不同:
1、位段的成员必须是 int
、unsigned int
或 signed int
.
2、位段的成员名后边有一个冒号和一个数字.
Bit field example
struct A
{
int _a : 2;
int _b : 5;
int _c : 10;
int _d : 30;
};
A 就是一个位段类型.
那位段 A 的大小是多少?
我们用 sizeof 看一下
为什么是 8 呢?
其实 位段 的 位 是指 二进制位;
int _a : 2
的意思是:_a
只占 2 个 bit 位;
int _b : 5
的意思是:_b
只占 5 个 bit 位;
int _c : 10
的意思是:_c
只占 10 个 bit 位;
int _d : 30
的意思是:_d
只占 30 个 bit 位;
那把所有的 bite The bits add up to 47 个 bite 位,给 6(48 bit) Bytes are enough!
为什么打印出来的是 8 个字节?也就是 64 个 bit 呢?
2. 位段的内存分配
1、 位段的成员可以是 int unsigned int
、signed int
或者是 char
(属于整形家族)类型
2、位段的空间上是按照需要以 4 个字节( int )或者 1 个字节( char )的方式来开辟的.
3、位段涉及很多不确定因素,位段是不跨平台的,注重可移植的程序应该避免使用位段.
Just look at this code
struct A
{
int _a : 2;
int _b : 5;
int _c : 10;
int _d : 30;
};
首先,_a
是整型,不管三七二十一,先开辟 4 个字节也就是 32 bit 给它,但是 _a
只需要 2 个 bit 呀,so take it 2 bit 以后,还剩 30 bit;
然后 _b
需要 5 bit,so take it 5 bit 以后,还剩下 25 bit;
其次 _c
需要 10 bit,so take it 10 bit 以后,还剩下 15 bit;
最后 _d
需要 30 bit,But there's still 15 bit 呀,完全不够,那怎么办呢?Open it up separately 4 个字节也就是 32 bit 给 _d
用(The rest of the above 15 bit won't use it again);
所以 sizeof when asking for size,也就是 8 字节.
上面解释了 整型 bit segment development,Then if the members of the structure are char 类型呢?
代码示例
struct S
{
char a : 3;
char b : 4;
char c : 5;
char d : 4;
};
首先,a
是 char 类型,不管三七二十一,先开辟 1 个字节也就是 8 bit 给它,但是 a
只需要 3 个 bit 呀,so take it 3 bit 以后,还剩 5 bit;
然后 b
需要 4 bit,所以从 5 bit take it inside 4 bit 以后,还剩下 1 bit;
其次 c
需要 5 bit,But there's still 1 bit 呀,完全不够,Open it up separately 1 个字节也就是 8 bit 给 c
用,so take it 5 bit 以后,还剩 3 bit;
最后 d
需要 4 bit,But there's still 3 bit 呀,完全不够,Then open it up separately 1 个字节也就是 8 bit 给 d
用(The rest of the above 3 bit won't use it again);
所以 sizeof when asking for size,也就是 3 字节.
我们用 sizeof 看一下
Now that you have learned the bit segment,Then let's analyze the following code
struct S
{
char a : 3;
char b : 4;
char c : 5;
char d : 4;
};
int main()
{
struct S s = {
0 };
s.a = 10;
s.b = 12;
s.c = 3;
s.d = 4;
return 0;
}
We have been calculated,This structure opens up 3 个字节,Then in the main function put this s 初始化为 0,这样就意味着,这 3 个字节里面,每一个 bit 位都为 0;
Primarily for a 开辟 1 个字节,a 只占其中的 3 bit,假设 a 在 8 bit 中,is used from low to high,That is, from right to left,现在把 10 赋值给 a,10 的二进制是 1010,但是 a 的空间大小是 3 bit,所以只能存 3 bit,that is to say 010,最高位的 1 省去
b 里面要放 12,12 的二进制是 1100,但是 b 刚好有 4 bit,所以直接放进去,此时还剩下 1 bit,完全不够 c 使用了,Then open up 1 字节
c 占 5 bit,现在要把 3 放进去,3 的二进制是 011,但是 c 要占 5 bit 呀,So in front of tian 0,也就是放 00011,此时还剩下 3 bit,完全不够 d 使用了,d 占 4 bit Then open up 1 字节
d 占 4 bit,现在要把 4 放进去,4 的二进制是 100,但是 c 要占 4 bit 呀,So in front of tian 0,也就是放 0100
At this point, the code in the main function has been completed.,So what exactly is stored in memory??
Because binary is not good to watch,We convert to hexadecimal
So does this actually exist in memory??We can debug and take a look
3. 位段的跨平台问题
1、 int
位段被当成有符号数还是无符号数是不确定的.
2、位段中最大位的数目不能确定.(16位机器最大16,32位机器最大32,写成27,在16位机
器会出问题.)
3、位段中的成员在内存中从左向右分配,还是从右向左分配标准尚未定义.
4、当一个结构包含两个位段,第二个位段成员比较大,无法容纳于第一个位段剩余的位时,是
舍弃剩余的位还是利用,这是不确定的.
总结:跟结构相比,位段可以达到同样的效果,但是可以很好的节省空间,但是有跨平台的问题存在.
4. 位段的应用
So where is the bit segment applied??For example, the classic in the following network IP 数据报
边栏推荐
- Flink Learning 11: Flink Program Parallelism
- 2022.7.29好题选讲(计数专题)
- It turns out that Maya Arnold can also render high-quality works!Awesome Tips
- window.open 全屏展示
- Shiny02---Shiny exception solution
- An IP conflict is reported after installing the software on a dedicated computer terminal
- Game Thinking 19: Multi-dimensional calculation related to games: point product, cross product, point-line-surface distance calculation
- 【无标题】长期招聘硬件工程师-深圳宝安
- 【结构体内功修炼】结构体实现位段(二)
- Stored procedure writing experience and optimization measures
猜你喜欢
随机推荐
Summary of Text Characterization Methods
爬虫之验证码
长期招聘嵌入式开发-深圳宝安
达梦数据库大表添加字段
Re regular expressions
openSource 知:社区贡献
TRACE32——通用寄存器查看与修改
unity urp 渲染管线顶点偏移的实现
691. 立方体IV
U++ 创建UI
Discourse 清理存储空间的方法
高效使用数码相机的诀窍
Embedded Systems: Basic Timers
Antdesign a-select 下拉框超出长度换行显示
JVM运行流程,运行时数据区,类加载,垃圾回收,JMM解析
力扣每日一题
DataFrame在指定位置插入行和列
Adb 授权过程分析
常用的遍历map的方法
线性代数对角化