当前位置:网站首页>c语言之位域
c语言之位域
2022-07-29 14:58:00 【悟空不买菜了】
位域的概念
数据在存储的时候,并不需要占用一个完整的单元,只需要占用一个或者几个二进制位,限定数据的位数,节约内存资源
简单来说就是,比如一个int,一般来说占用四个字节,那么四个字节就是32个位,但是我们实际当中用到的数据用不了那么多位,比如我们要存储月份,那么月份是不是就是1-12呢,那么我们这个时候就可以把这个int类型设计成四个位,那么它能存储最大的数据就是1111,对吗,也就是0-15的范围,存储2^4个数据,那么这个范围就是我们想要的合理范围。
在c语言中,位域一般用于结构体里面
话不多说,直接上代码
#include <stdio.h>
#include <stdlib.h>
struct Data
{
unsigned int num1 : 1;//1个位 ,二进制为1,也就是只能表示0或者1
unsigned int num2 : 2;//2个位,二进制为00或者11 0->3
unsigned int num3 : 3;//3个位 000 111 0->7
};
void main()
{
printf("%d\n", sizeof(struct Data));
system("pause");
}
运行结果:
后面再来说说怎么计算位域结构体的大小,再说一点就是,位域就相当于是限制了数据大小,不可越界,比如下面的代码
#include <stdio.h>
#include <stdlib.h>
struct Data
{
unsigned int num1 : 1;//1个位 ,二进制为1,也就是只能表示0或者1
unsigned int num2 : 2;//2个位,二进制为00或者11 0->3
unsigned int num3 : 3;//3个位 000 111 0->7
};
void main()
{
struct Data data1;
data1.num1 = 2;
printf("%d\n", data1.num1);
system("pause");
}
上面会打印结果:
一旦越界就不会打印我们想要的结果。
如何判断带有位域的结构体大小
#include <stdio.h>
#include <stdlib.h>
union MyUnion1
{
char bj[5];//5
int num;
};
union MyUnion2
{
char bj[5];
int num[2];
};
struct stu
{
//联合体的大小就是占用最大的数据类型大小,但必须被最宽基本类型整除
union{
char bj[5];
int bh[2];//
}class;//8个字节//0 8
char xm[8];//8 8
float cj;//16 4
};//16 + 4 = 20
//这里按照结构体字节对齐来考虑
struct test
{
char f1 : 3;//0 1
short f2 : 4;//2 2
char f3 : 5;//4 1
};//5->6 才是short的整数倍
struct test1
{
//同类型可以重合
char num1 : 6;
char num2 : 3;
//上面占2个字节
//下面重合占了四个字节
int num3 : 5;
int num5 : 20;//2-》4 + 4 = 8
};//8
struct test2{
char f1 : 3;//0 1
char f2;//非位域字段1 1//不用考虑重合
char f3 : 5;//2 1 ->3
};//3
int main() {
printf("%d\n", sizeof(struct test));
printf("%d\n", sizeof(struct test1));
printf("%d\n",sizeof(struct test2));
getchar();
return 0;
}
运行结果:
如何用位域来读取一个整数、浮点数的二进制位
1.先来看读取一个整数
#include <stdio.h>
#include <stdlib.h>
struct bit
{
unsigned char ch1 : 1;
unsigned char ch2 : 1;
unsigned char ch3 : 1;
unsigned char ch4 : 1;
unsigned char ch5 : 1;
unsigned char ch6 : 1;
unsigned char ch7 : 1;
unsigned char ch8 : 1;
};//数据类型相同占用重合1个字节,刚好八个位
void main()
{
int num = 2;
struct bit *p = #//指向int类型的首地址
int length = 4;//int占四个字节,也就是4
//4 3 2 1
while (length--) {
//注意,数据从右往左解析的
printf("%d%d%d%d %d%d%d%d ",
(p + length)->ch8,
(p + length)->ch7,
(p + length)->ch6,
(p + length)->ch5,
(p + length)->ch4,
(p + length)->ch3,
(p + length)->ch2,
(p + length)->ch1);
}
system("pause");
}
运行结果
2.再来看一个浮点数的读取
#include <stdio.h>
#include <stdlib.h>
struct bit
{
unsigned char ch1 : 1;
unsigned char ch2 : 1;
unsigned char ch3 : 1;
unsigned char ch4 : 1;
unsigned char ch5 : 1;
unsigned char ch6 : 1;
unsigned char ch7 : 1;
unsigned char ch8 : 1;
};//数据类型相同占用重合1个字节,刚好八个位
void main()
{
float num = 19.625f;
struct bit *p = #//指向int类型的首地址
int length = 4;
//4 3 2 1
while (length--) {
printf("%d%d%d%d %d%d%d%d ",
(p + length)->ch8,
(p + length)->ch7,
(p + length)->ch6,
(p + length)->ch5,
(p + length)->ch4,
(p + length)->ch3,
(p + length)->ch2,
(p + length)->ch1);
}
printf("%p\n", p);
system("pause");
}
运行结果:
看一下浮点数内存图
边栏推荐
- 使用Xshell和Xftp7跑学校服务器记录
- arcpy脚本制作arcgis工具箱注意事项
- 如何在MySQL中执行SQL?
- Couldn‘t create temporary file /tmp/apt.conf.uko4Kd for passing config to apt-key
- DevOps的未来趋势
- 不会多线程还想进BAT?精选19道多线程面试题,有答案边看边学
- Work Efficiency - Fifteen minutes allows you to quickly learn Markdown syntax to proficient in typesetting practice notes
- 从通信延伸到全行业,亚信科技AntDB 7.0蓄势待发
- redis常见面试题(背诵篇)
- 瑞萨RZ/G2L处理器详细测评
猜你喜欢
微服务实战|集中配置中心Config非对称加密与安全管理
Google Play 政策更新 | 2022 年 7 月
NLP自然语言处理-机器学习和自然语言处理介绍(一)
Realization of Online Chat System Based on SSM
腾讯云数据库负责人林晓斌借1亿炒股:已爆仓破产
The raised platform system based on JSP&Servlet implementation
Why does APP use the JSON protocol to interact with the server: serialization related knowledge
LeetCode·621.任务调度器·构造法
【IIC通信】Chap.1(I2C)IIC通信原理、IIC读写时序详解
NLP自然语言处理-机器学习和自然语言处理介绍(三)
随机推荐
【LeetCode】53. 最大子数组和
Learning Policies for Adaptive Tracking with Deep Feature Cascades全文翻译
KDD'22博士论文奖:清华裘捷中成亚洲高校首位获得者,斯坦福Rex Ying获WINNER奖
Shell脚本编程-运算
论人生自动化
正则、grep/egrep、sed、awk
数据分析(一)
dedecms编辑器支持pdf导入
QT通过UDP分包传输大图像(测试可传6M)
稳稳当当的生活
AVH部署实践 (一) | 在Arm虚拟硬件上部署飞桨模型
深陷盈利困境,“寒冬”中也要二次递表,北森上市迫切
[yolov7 series two] positive and negative sample allocation strategy
【LeetCode】566. 重塑矩阵
【LeetCode】350. 两个数组的交集 II
AC自动机笔记与例题整理
自动化配置SSH免密登录和取消SSH免密配置脚本
MySQL Index Common Interview Questions (2022 Edition)
【深度学习】深度学习刷SOTA的一堆trick
I quit my job after cutting the brothers, and turned to do a small clerk