当前位置:网站首页>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位机
器会出问题。 - 位段中的成员在内存中从左向右分配,还是从右向左分配标准尚未定义。
- 当一个结构包含两个位段,第二个位段成员比较大,无法容纳于第一个位段剩余的位时,是
舍弃剩余的位还是利用,这是不确定的。
- 总结:跟结构相比,位段可以达到同样的效果,但是可以很好的节省空间,但是有跨平台的问题存在
边栏推荐
- TS类型体操 之 字符串的妙用
- On why we should program for all
- 软件开发的一点随记
- Opencv learning notes 8 -- answer sheet recognition
- WebRTC系列-H.264预估码率计算
- Compliance and efficiency, accelerate the digital transformation of pharmaceutical enterprises, and create a new document resource center for pharmaceutical enterprises
- Uibehavior, a comprehensive exploration of ugui source code
- The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
- NFT smart contract release, blind box, public offering technology practice -- contract
- edge浏览器 路径获得
猜你喜欢
Basics of reptile - Scratch reptile
Database basic commands
Step by step guide to setting NFT as an ens profile Avatar
08- [istio] istio gateway, virtual service and the relationship between them
In the era of digital economy, how to ensure security?
. Net 6 learning notes: what is NET Core
21. Delete data
好用的TCP-UDP_debug工具下载和使用
解决方案:智慧工地智能巡检方案视频监控系统
"Designer universe": "benefit dimension" APEC public welfare + 2022 the latest slogan and the new platform will be launched soon | Asia Pacific Financial Media
随机推荐
PHP - Common magic method (nanny level teaching)
数字经济时代,如何保障安全?
File upload of DVWA range
Qualitative risk analysis of Oracle project management system
24. Query table data (basic)
P3047 [usaco12feb]nearby cows g (tree DP)
Webrtc series-h.264 estimated bit rate calculation
Data governance: Data Governance under microservice architecture
PHP Coding Standard
Understanding of law of large numbers and central limit theorem
MFC 给列表控件发送左键单击、双击、以及右键单击消息
On why we should program for all
[count] [combined number] value series
数据治理:数据质量篇
C # display the list control, select the file to obtain the file path and filter the file extension, and RichTextBox displays the data
上线APS系统,破除物料采购计划与生产实际脱钩的难题
Luogu p1836 number page solution
二叉树创建 & 遍历
[t31zl intelligent video application processor data]
Golang DNS write casually