当前位置:网站首页>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
边栏推荐
猜你喜欢
随机推荐
自定义fingerprint特征
韦伯首批照片引发论文竞速大战:晚13秒即错失首发,科研党纷纷肝得起飞
Shell脚本编程-运算
qt vs2015中无法打开源文件“QtWidgets”的解决方案
【 LeetCode 】 88. Merging two orderly array
Flink SQL Hudi 实战
【7.22-7.29】写作社区精彩技术博文回顾
Micro combat | centralized configuration service center Config asymmetric encryption and security management
gateway基本使用
【LeetCode】53. 最大子数组和
求教一下 现在最新版的flinkcdc能获取到oracle的ddl变更信息吗?
[yolov7 series two] positive and negative sample allocation strategy
Principles Of Mathematical Analysis, Third Edition免费下载地址
网络知识大集合(最详细)与网络通信过程
数据库管控平台-awr报告采集(mysql/oracle)
MySQL索引常见面试题(2022版)
工作效率-十五分钟让你快速学习Markdown语法到精通排版实践备忘
C语言 3:常量和变量,顺序语句,选择语句,循环语句,作用域和生存期
第十九届同济大学程序设计竞赛暨高校网络友谊赛 G-归零(可持久化权值线段树)
NLP自然语言处理-机器学习和自然语言处理介绍(二)









