当前位置:网站首页>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
边栏推荐
- Functions and parameters
- 12.数据仓库搭建之ADS层搭建
- The latest news of spring recruitment in 2022: the average salary of it Internet industry is 18500 yuan
- 10.数据仓库搭建之DWD层搭建
- How to deal with the mismatch between subtitle files and video files
- What are the B domain, m domain and O domain
- Data visualization
- Easypoi excel simple export
- redis 源码分析3 间断遍历的实现
- What is the employment prospect of software testing? There is a large demand for talents and strong job stability
猜你喜欢

线上软件测试培训机构柠檬班与iTest.AI平台达成战略合作

C语言&位域

What is the employment prospect of software testing? There is a large demand for talents and strong job stability

聊聊写代码的20个反面教材

Online software testing training institutions lemon class and itest AI platform achieves strategic cooperation

Talk about the 8 pits of redis distributed lock

Buuctf miscellaneous - QR code

聊聊并发编程的12种业务场景

MySQL安装配置教程(超级详细)

Redis source code analysis - data structure and Implementation (Dictionary dict)
随机推荐
gradle
使用Flink SQL传输市场数据1:传输VWAP
用户态协议栈-基于netmap的UDP实现
2022年春招最新消息:IT互联网行业平均薪资18500元
Redis source code analysis - data structure and Implementation (Dictionary dict)
Rxjs source code analysis (I) observable
Is the cookie valid for a limited time? How to set cookies? Teach you to set by hand
Easypoi excel simple export
用Flink SQL流化市场数据2:盘中风险价值
2020-10-22
Questions d'entrevue courantes du système d'exploitation
redis源码分析 2 迭代器
MySQL安装配置教程(超级详细)
Excel imports long data and changes to 000 at the end
[bug solution] org apache. ibatis. type. TypeException: The alias ‘xxxx‘ is already mapped to the value ‘xxx‘
线上软件测试培训机构柠檬班与iTest.AI平台达成战略合作
2020-11-10
MySQL - index
【全网首发】一个月后,我们又从 MySQL 双主切换成了主-从
ETL tool -- kettle realizes simple data migration