当前位置:网站首页>C language & bit field
C language & bit field
2022-07-19 05:30:00 【Learn to put down ta】
Bit field ( Binary digit )
effect : Can name a byte and eight bits separately and use , Significantly improve memory utilization
use : Single chip microcomputer
Definition : When defining a structure, add : Write the position occupied
Regulations : The width of the bit field cannot exceed the length of the attached data type
struct Text{
unsigned int a:2;
unsigned int b:1;
};
A structure defined here , Inside a Account for only a 2 Binary digit ,b Account for only a 1 position , The whole structure only accounts for 3 position , Cannot exceed the structure data type length interpretation :unsigned int Type account 4 Bytes , altogether 32 Bit binary , You can't give a Assignment proportion 33 position .
Nameless bit field
struct Text{
unsigned int a:2;
unsigned int b:1;
unsigned int :1;
};
It's a bit field without a name , The function is mostly to adjust the position of variables , Round up the whole byte
Tips :
Support the type of bit field : int、signed int and unsigned int,_Bool
Other compilers have extensions , Can support more types .
Logical bit operators ( In order of priority )
~ According to the not : Put the... Of the operand 1 Turn into 0,0 Turn into 1;
& Bitwise AND : The corresponding bit operands on both sides are 1 The result is true, Otherwise false
^ Bitwise XOR : When the corresponding operands on both sides are different, it is 1, Otherwise 0
| Press bit or : One of the operands on both sides is 1 The results for 1, Otherwise 0
Besides ~ Cannot be associated with = combination , Everything else can be compared with = combination , Usage is similar. +=
int mask = 0xff;
int a = 0xabcd;
int b = 0xbbbb;
a &= mask;// equivalent a = a & mask;
b ^= mask;//b = b ^ mask;
Shift Operators
1、 Left shift operator : There are two parameters ( Binary number to be shifted )<<( Shift left digit ), Fill the blank after moving , Move a few bits to the left and multiply the source operand by 2 Several power
11001010 << 2 == 00101000
2、 Shift right operator : Shift the source operand a few bits to the right by 2 Several power
11001010 >> 2 == 00110010
Be careful : The above instructions are limited to unsigned integers on the left , The right shift is not greater than the maximum left width , Other behaviors and undefined behaviors are treated differently
application
1、 Judgment bit : Judge whether the number is 1, For example, judgment 1100 1010 Whether the first and third places of are 1 We can use a mask 1010 0000 Bitwise and with the source operand , If the result is 1010 0000 Then the judgment is true Otherwise false.
2、 open ( close ) position : Which digit can be set as 1( perhaps 0), Such as the third place , Also use mask 0010 0000(1101 1111) Bitwise OR... With the source operand |(&), The result is to put the third position 1(0).
11001010 | 00100000 == 11101010
3、 Transpose : You can transpose the number , For example, mask the third and fifth bits 0010 1000 XOR with the source operand ^, Just transpose the third and fifth positions
11001010 ^ 00101000 == 11100010
边栏推荐
猜你喜欢

Shell script configures root to login to other hosts without secret

BUUCTF web WarmUp
![[AI] action recognition using simple neural network -- Based on coco key points](/img/67/cd6be6e070fb5d4d44ee043ebd7fac.png)
[AI] action recognition using simple neural network -- Based on coco key points

How to deal with the mismatch between subtitle files and video files

mysql的事务

指针数组&数组指针

操作系统常见面试题

Parent components plus scoped sometimes affect child components

使用Flink SQL传输市场数据1:传输VWAP

Data Lakehouse的未来-开放
随机推荐
How to deal with the mismatch between subtitle files and video files
分布式存储-fastdfs
Easypoi之excel简单导出
12.数据仓库搭建之ADS层搭建
mysql的事务
List与Map
Talk about the 8 pits of redis distributed lock
H5 how to obtain intranet IP and public IP
云服务器部署WEB项目
MySQL安装配置教程(超级详细)
MySQL - index
ETL工具——kettle实现简单的数据迁移
Pointer advanced simple summary
Redis source code analysis 3 implementation of discontinuous traversal
表字段属性查询
2.6.2 memory leakage
MySQL 查询当天、本周,本月、上一个月的数据
Router loopback port experiment
Swagger configuration and use
线程池如何监控,才能帮助开发者快速定位线上错误?