当前位置:网站首页>C language - bitwise operations
C language - bitwise operations
2022-07-30 11:17:00 【Embedded Pit Notes】
C语言中,Especially in embedded development,Bit manipulation is a very common knowledge point,Bitwise is involved(bit)The place of operation is also very common.
This article shares someCThe basics of bit manipulation operations in the language.
1、 位与操作( & )
First, the bitwise AND must be distinguished(&)和逻辑与(&&),There seems to be a difference between the two,But the final result is completely different.
位与 & 的真值表:
从上表可以看出,位与 & The operating principle is :Only if both objects involved in the operation are true(true 或 1)时,The result of the operation is obtained true 或 1 ,其它的都为flase 或 0.
位与 & In fact, it is to convert the two numbers involved in the operation into binary first,Then do the AND to get the result after the operation,如下:
比如:0x01 & 0x03 = 0x01 // 运算如下:
0x01:0 0 0 1
0x03:0 0 1 1
结果:0 0 0 1
2、 位或操作 ( | )
First, the bitwise AND must be distinguished(|)和逻辑与(||),There seems to be a difference between the two,But the final result is completely different.
位与 | 的真值表:
从上表可以看出,位与 | The operating principle is :As long as one of the two objects involved in the operation is true(true 或 1)时,The result of the operation is obtained true 或 1 ,只有两个都是 0 才为 flase 或 0.
位与 | In fact, it is to convert the two numbers involved in the operation into binary first,Then do the AND to get the result after the operation,如下:
比如:0x01 & 0x03 = 0x03 // 运算如下:
0x01:0 0 0 1
0x03:0 0 1 1
结果: 0 0 1 1
3、位取反操作(~)
Bit inversion is to convert the hexadecimal number to binary first,Then invert the corresponding bit(1 取反变为 0,0 取反变为1).比如:
比如:0x01 、 0x03 // 运算如下:
0x01:0 0 0 1 ~ 0x01:1 1 1 0 = 0x0D
0x03:0 0 1 1 ~ 0x03:1 1 0 0 = 0x0C
4、位异或操作( ^ )
位异或 ^ 的真值表:
从上表可以看出,位异或 ^ The operating principle is :When one of the two objects involved in the operation is true(true 或 1)时,The result of the operation is obtained true 或 1 ;如果两个都是 0 就为 flase 或 0;两个都是 1 就为 flase 或 0.总而言之就是:相同为0,不同为1.
比如:0x01 ^ 0x03 = 0x02 // 运算如下:
0x01:0 0 0 1
0x03:0 0 1 1
结果:0 0 1 0
5、位移操作(<< 、>>)
displacement operation,Shift a few bits to the left to add a few bits to the right0,Shift a few bits to the right to add a few bits to the left0,The part outside the numerical range is discarded.如下:
比如:0x03 // 运算如下:
0x03:0 0 1 1 右移1位:0 1 1 0
0x03:0 0 1 1 右移2位:1 1 0 0
0x03:0 0 1 1 右移3位:1 0 0 0
0x03:0 0 1 1 左移1位:0 0 0 1
0x03:0 0 1 1 左移2位:0 0 0 0
6、Practical use of bit manipulation
6.1、Clear specific bits to zero
If you want to clear a bit of a specific number(置零),Use bitwise AND & 进行操作.比如:
比如:0x0A = 1 0 1 0
Want to clear the second bit,The other bits remain unchanged,with bit and & 操作如下:
方法1:0x0A & 0x0D = 0x08
0x0A = 1 0 1 0
0x0D = 1 1 0 1
结果 :1 0 0 0 = 0x08
方法二:0x0A & ~(1<<1)
6.2、to a specific location 1
If you want to clear a bit of a specific number(置零),Use bitwise AND & 进行操作.比如:
比如:0x0A = 1 0 1 0
Want to be the first position 1,The other bits remain unchanged,with bit and | 操作如下:
方法1:0x0A | 0x01 = 0x0B
0x0A = 1 0 1 0
0x01 = 0 0 0 1
结果 :1 0 1 1 = 0x0B
方法二:0x0A | (1<<0)
6.3、特定位取反
If you want to invert a bit of a specific number,使用异或 ^ 进行操作.比如:
比如:0x0A = 1 0 1 0
I want to negate the first bit,The other bits remain unchanged,Use bitwise XOR ^ 操作如下:
0x0A ^ 0x01 = 0x0B
0x0A = 1 0 1 0
0x01 = 0 0 0 1
结果 :1 0 1 1 = 0x0B
注意:The XOR operation is the same as zero,不同为1operating principle!
6.4、Get a specific binary bit
If you want to get one or a few specific bits in a number,可以参考如下操作:
比如:0x0A = 1 0 1 0,Want to get the value of the second and third bits,可以按下面方法操作:
T = (0x0A >> 1) & 0x03 The resulting binary value is 0 1
If you want to get the fourth place,如下:
T = (0x0A >> 3) & 0x01 The resulting binary value is 1
其他的操作类似.
如果对嵌入式技术感兴趣的,欢迎关注微信公众号“嵌入式之入坑笔记”,一起学习讨论啊!
边栏推荐
- 数据库事务,JDBC操作和数据类型
- Detailed explanation of @RequestBody and @ResponseBody
- mysql分页查询倒序_【Mysql笔记】MySQL实现分页查询[通俗易懂]
- Oracle中SQL语言和分页rownum分析
- Neural Network Study Notes 4 - Autoencoder (including sparse, stacked) (updated)
- 易基因:人类tRNA基因位点表现出与衰老相关的DNA高甲基化|研究文章
- Drools 规则引擎一文读懂
- unity3d C#语言基础(继承)
- JSP 语法简介说明
- "Learning Cloud Networking with Teacher Tang" - Problem Location - The host is working but the container is not working
猜你喜欢
随机推荐
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
feign远程调用时如何在请求头加入数据
编译Hudi
鸿湖万联扬帆富设备开发板正式合入OpenHarmony主干
Log4j additivity属性简介说明
神经网络学习笔记3——LSTM长短期记忆网络
SQL language and paging rownum analysis in Oracle
数据库事务,JDBC操作和数据类型
京东校招笔试题+知识点总结
UE5 GAS Study Notes Postscript 0
vscode中写markdown格式笔记的配置过程和相关语法
【C和指针第七章】可变参数列表
AB测试 总结归纳
FPGA刷题——计数器(简易秒表、可置位计数器、加减计数器)
Performance testing of API Gateway APISIX on Google Cloud T2A and T2D
干货|语义网、Web3.0、Web3、元宇宙这些概念还傻傻分不清楚?(中)
ODrive应用 #4 配置参数&指令「建议收藏」
HJY-F931A/YJ三相电压继电器
美团内推+校招笔试题+知识点总结
Verilog之数码管译码









