当前位置:网站首页>C语言 - 位段
C语言 - 位段
2022-07-06 07:57:00 【欧橘猫】
位段
- 什么是位段?
C语言允许在一个结构体中以比特位(bit)为单位来指定其成员所占内存长度
这种以位为单位的成员称为位段。利用位段能够用较少的位数存储数据。
例题:
上面的
_a : 2
意思是占 2 个bit位_b : 5
意思是占 5 个bit位_c : 10
意思是占 10 个bit位_d : 30
意思是占 30 个bit位复习一下知识:
8 bit = 1 字节
4 字节 = 一个整形(int)
上面所有成员加起来也就是 47 bit,大概就是 二个整形的大小
那答案为什么是 8 ?
那么上面的例题中的结构体中位段是怎么分配空间的呢?
位段的内存分配
- 位段的成员可以是 int unsigned int signed int 或者是 char (属于整形家族)类型
- 位段的空间上是按照需要以4个字节( int )或者1个字节( char )的方式来开辟的。
- 位段涉及很多不确定因素,位段是不跨平台的,注重可移植的程序应该避免使用位段
- 位段占的二进制位数不能超过该基本类型所能表示的最大位数
即位段不能跨字节存储。例如:int是占4个字节,那么最多只能是32 bit位,不能超过它
例题:
struct S
{
char a:3;
char b:4;
char c:5;
char d:4;
};
int main()
{
struct S n = {
0 };
n.a = 10;
n.b = 12;
n.c = 3;
n.d = 4;
return 0;
}
图解:
这是在 vs 2022编译器测试数据
- 之前说过
位段的空间上是按照4个字节( int )或者1个字节( char )的方式来开辟的
所以一次是开辟 4个int 类型的空间 - 位段在
一个字节
内部的数据是先使用低地址
在使用高地址
,从右向左使用(char 类型)
如果是 int 类型,是一个整形从右向左 - 当一块空间里剩余的空间不够下一个成员使用时,会浪费掉
- 在位段中不给其起名字,无法访问该位段,但是计算大小是仍占空间
- 不可对位段进行 &取地址 操作
位段的跨平台问题
- int 位段被当成有符号数还是无符号数是不确定的。
- 位段中最大位的数目不能确定
例如:16位机器最大16,32位机器最大32,写成27,在16位机
器会出问题。 - 位段中的成员在内存中从左向右分配,还是从右向左分配标准尚未定义。
- 当一个结构包含两个位段,第二个位段成员比较大,无法容纳于第一个位段剩余的位时,是
舍弃剩余的位还是利用,这是不确定的。
- 总结:跟结构相比,位段可以达到同样的效果,但是可以很好的节省空间,但是有跨平台的问题存在
边栏推荐
- MFC 给列表控件发送左键单击、双击、以及右键单击消息
- National economic information center "APEC industry +": economic data released at the night of the Spring Festival | observation of stable strategy industry fund
- Data governance: metadata management
- 解决方案:智慧工地智能巡检方案视频监控系统
- Epoll and IO multiplexing of redis
- [非线性控制理论]9_非线性控制理论串讲
- Entity class design for calculating age based on birthday
- PHP - Common magic method (nanny level teaching)
- Interview Reply of Zhuhai Jinshan
- Golang DNS 随便写写
猜你喜欢
Solution: intelligent site intelligent inspection scheme video monitoring system
[computer skills]
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
Basics of reptile - Scratch reptile
Asia Pacific Financial Media | designer universe | Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers
23. Update data
【T31ZL智能视频应用处理器资料】
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
Opencv learning notes 9 -- background modeling + optical flow estimation
Inspiration from the recruitment of bioinformatics analysts in the Department of laboratory medicine, Zhujiang Hospital, Southern Medical University
随机推荐
[computer skills]
861. Score after flipping the matrix
Launch APS system to break the problem of decoupling material procurement plan from production practice
edge浏览器 路径获得
File upload of DVWA range
Uibehavior, a comprehensive exploration of ugui source code
Data governance: metadata management
Type of data in energy dashboard
DataX self check error /datax/plugin/reader/_ drdsreader/plugin. Json] does not exist
Flash return file download
数据治理:数据质量篇
649. Dota2 Senate
Mex related learning
[factorial inverse], [linear inverse], [combinatorial counting] Niu Mei's mathematical problems
Learn Arduino with examples
It's hard to find a job when the industry is in recession
Simulation of Teman green interferometer based on MATLAB
数据治理:元数据管理篇
MES, APS and ERP are essential to realize fine production
CAD ARX 获取当前的视口设置