当前位置:网站首页>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位机
器会出问题。 - 位段中的成员在内存中从左向右分配,还是从右向左分配标准尚未定义。
- 当一个结构包含两个位段,第二个位段成员比较大,无法容纳于第一个位段剩余的位时,是
舍弃剩余的位还是利用,这是不确定的。
- 总结:跟结构相比,位段可以达到同样的效果,但是可以很好的节省空间,但是有跨平台的问题存在
边栏推荐
- Rust language - receive command line parameter instances
- Yu Xia looks at win system kernel -- message mechanism
- 数据治理:微服务架构下的数据治理
- [Yugong series] February 2022 U3D full stack class 010 prefabricated parts
- The difference between TS Gymnastics (cross operation) and interface inheritance
- http缓存,强制缓存,协商缓存
- Nft智能合约发行,盲盒,公开发售技术实战--合约篇
- 解决方案:智慧工地智能巡檢方案視頻監控系統
- "Designer universe" Guangdong responds to the opinions of the national development and Reform Commission. Primary school students incarnate as small community designers | national economic and Informa
- Description of octomap averagenodecolor function
猜你喜欢
Comparison of usage scenarios and implementations of extensions, equal, and like in TS type Gymnastics

Risk planning and identification of Oracle project management system

"Designer universe" APEC design +: the list of winners of the Paris Design Award in France was recently announced. The winners of "Changsha world center Damei mansion" were awarded by the national eco
TS 类型体操 之 extends,Equal,Alike 使用场景和实现对比
![datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在](/img/17/415e66d67afb055e94a966de25c2bc.png)
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在

Codeforces Global Round 19(A~D)

Nft智能合约发行,盲盒,公开发售技术实战--合约篇

珠海金山面试复盘
![[count] [combined number] value series](/img/f5/6fadb8f1c8b75ddf5994c2c43feaa6.jpg)
[count] [combined number] value series

"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
随机推荐
解决方案:智慧工地智能巡檢方案視頻監控系統
Step by step guide to setting NFT as an ens profile Avatar
opencv学习笔记九--背景建模+光流估计
Simulation of Michelson interferometer based on MATLAB
xpath中的position()函数使用
Pangolin Library: control panel, control components, shortcut key settings
Nft智能合约发行,盲盒,公开发售技术实战--合约篇
861. Score after flipping the matrix
MES, APS and ERP are essential to realize fine production
Simulation of holographic interferogram and phase reconstruction of Fourier transform based on MATLAB
Entity class design for calculating age based on birthday
datax自检报错 /datax/plugin/reader/._drdsreader/plugin.json]不存在
Key value judgment in the cycle of TS type gymnastics, as keyword use
Mex related learning
Golang DNS write casually
解决方案:智慧工地智能巡检方案视频监控系统
Position() function in XPath uses
Oracle time display adjustment
TS类型体操 之 字符串的妙用
TS 体操 &(交叉运算) 和 接口的继承的区别