当前位置:网站首页>bit field in c language
bit field in c language
2022-07-29 15:34:00 【Goku doesn't buy groceries】
位域的概念
数据在存储的时候,It is not necessary to occupy a complete unit,Only need to occupy one or a few binary bits,Limit the number of bits of data,节约内存资源
简单来说就是,比如一个int,Usually takes up four bytes,Then four bytes are32个位,But the data we actually use doesn't use that many bits,For example, we want to store the month,Then the month is not1-12呢,Then we can put this at this timeintType is designed to be four bits,Then it can store the largest data is1111,对吗,也就是0-15的范围,存储2^4个数据,Then this range is the reasonable range we want.
在c语言中,Bit fields are generally used in structures
话不多说,直接上代码
#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");
}运行结果:

Let's talk about how to calculate the size of the bit field structure later,再说一点就是,Bit fields are equivalent to limiting the data size,不可越界,比如下面的代码
#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");
}The result will be printed above:

Once out of bounds the result we want will not be printed.
How to determine the size of a struct with bitfields
#include <stdio.h>
#include <stdlib.h>
union MyUnion1
{
char bj[5];//5
int num;
};
union MyUnion2
{
char bj[5];
int num[2];
};
struct stu
{
//The size of the union is the size of the largest data type occupied,But must be divisible by the widest primitive type
union{
char bj[5];
int bh[2];//
}class;//8个字节//0 8
char xm[8];//8 8
float cj;//16 4
};//16 + 4 = 20
//Here, it is considered according to the byte alignment of the structure
struct test
{
char f1 : 3;//0 1
short f2 : 4;//2 2
char f3 : 5;//4 1
};//5->6 才是short的整数倍
struct test1
{
//The same type can overlap
char num1 : 6;
char num2 : 3;
//上面占2个字节
//The following overlap occupies four bytes
int num3 : 5;
int num5 : 20;//2-》4 + 4 = 8
};//8
struct test2{
char f1 : 3;//0 1
char f2;//non-bit field fields1 1//Don't worry about overlapping
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;
}运行结果:

How to read an integer using a bitfield、The binary digits of a floating point number
1.Let's first look at reading an integer
#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;
};//The same data type occupies the overlap1个字节,Exactly eight digits
void main()
{
int num = 2;
struct bit *p = #//指向intThe first address of the type
int length = 4;//int占四个字节,也就是4
//4 3 2 1
while (length--) {
//注意,Data is parsed from right to left
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.Let's look at the reading of a floating-point number
#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;
};//The same data type occupies the overlap1个字节,Exactly eight digits
void main()
{
float num = 19.625f;
struct bit *p = #//指向intThe first address of the type
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");
}运行结果:

Take a look at the floating point memory graph
边栏推荐
- Flink SQL Hudi 实战
- NLP自然语言处理-机器学习和自然语言处理介绍(二)
- 药品研发--质理研究人员绩效办法
- LeetCode·每日一题·593.有效的正方形·数学
- 使用Xshell和Xftp7跑学校服务器记录
- NLP自然语言处理-机器学习和自然语言处理介绍(三)
- 数据库管控平台-awr报告采集(mysql/oracle)
- 进入中国27年,又一美妆巨头要离场
- Jmeter实现多用户测试
- Micro combat | centralized configuration service center Config asymmetric encryption and security management
猜你喜欢
随机推荐
关于数字化转型 你需要知道的八项指导原则
[yolov7 series two] positive and negative sample allocation strategy
【yolov7系列二】正负样本分配策略
【LeetCode】53. 最大子数组和
【左连接】坑点
c语言字符和字符串总结
【IIC通信】Chap.1(I2C)IIC通信原理、IIC读写时序详解
文件管理:文件的物理结构
这个保护数据隐私的赛道,人人都想插一脚,互联网大厂挤破头,连甲方都下场自研了...
I/O代码实践
c语言之位域
How to get local json
ES6 从入门到精通 # 10:Set 集合数据类型
You need to know about the digital transformation of eight guiding principles
Generate Class bytecode files with Asm
文本处理之xml
又一位AI大佬辞职进体制内!AI的尽头是编制?
gateway基本使用
广汽本田安全驾驶体验营,老司机的必修课
【ArcGIS微课1000例】0030:ArcGIS利用MXD doctor工具分析并修复mxd地图文档









