当前位置:网站首页>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位机
器会出问题。 - 位段中的成员在内存中从左向右分配,还是从右向左分配标准尚未定义。
- 当一个结构包含两个位段,第二个位段成员比较大,无法容纳于第一个位段剩余的位时,是
舍弃剩余的位还是利用,这是不确定的。
- 总结:跟结构相比,位段可以达到同样的效果,但是可以很好的节省空间,但是有跨平台的问题存在
边栏推荐
- 实现精细化生产, MES、APS、ERP必不可少
- Linked list interview questions (Graphic explanation)
- [computer skills]
- Asia Pacific Financial Media | art cube of "designer universe": Guangzhou community designers achieve "great improvement" in urban quality | observation of stable strategy industry fund
- Flash return file download
- A Closer Look at How Fine-tuning Changes BERT
- 将 NFT 设置为 ENS 个人资料头像的分步指南
- A Closer Look at How Fine-tuning Changes BERT
- 23. Update data
- octomap averageNodeColor函数说明
猜你喜欢
解决方案:智慧工地智能巡檢方案視頻監控系統
[1. Delphi foundation] 1 Introduction to Delphi Programming
好用的TCP-UDP_debug工具下载和使用
"Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
Artcube information of "designer universe": Guangzhou implements the community designer system to achieve "great improvement" of urban quality | national economic and Information Center
[computer skills]
861. Score after flipping the matrix
Opencv learning notes 9 -- background modeling + optical flow estimation
Pre knowledge reserve of TS type gymnastics to become an excellent TS gymnastics master
[redis] Introduction to NoSQL database and redis
随机推荐
NFT smart contract release, blind box, public offering technology practice -- contract
Document 2 Feb 12 16:54
软件开发的一点随记
. Net 6 learning notes: what is NET Core
【Redis】NoSQL数据库和redis简介
opencv学习笔记八--答题卡识别
edge瀏覽器 路徑獲得
Solution: intelligent site intelligent inspection scheme video monitoring system
Opencv learning notes 9 -- background modeling + optical flow estimation
File upload of DVWA range
Esrally domestic installation and use pit avoidance Guide - the latest in the whole network
Helm install Minio
What are the ways to download network pictures with PHP
"Friendship and righteousness" of the center for national economy and information technology: China's friendship wine - the "unparalleled loyalty and righteousness" of the solidarity group released th
In the era of digital economy, how to ensure security?
Flash return file download
Sanzi chess (C language)
Interview Reply of Zhuhai Jinshan
TS 体操 &(交叉运算) 和 接口的继承的区别
数据治理:主数据的3特征、4超越和3二八原则