当前位置:网站首页>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
其他的操作类似.
如果对嵌入式技术感兴趣的,欢迎关注微信公众号“嵌入式之入坑笔记”,一起学习讨论啊!
边栏推荐
- 《跟唐老师学习云网络》 - 问题定位 - 主机通但容器不通
- 易基因:人类tRNA基因位点表现出与衰老相关的DNA高甲基化|研究文章
- Linux内核设计与实现(十)| 页高速缓存和页回写
- 【JZ64 求1+2+3+...+n】
- Telerik2022 R2,有效的自动化测试
- 实现web实时消息推送的7种方案
- Transfer Learning技术研修
- 鸿湖万联扬帆富设备开发板正式合入OpenHarmony主干
- Classes and Objects - 6 Default Member Functions
- Swift common extension classes and simple encapsulation
猜你喜欢
随机推荐
async.js入门
【云筑共创】华为云携手鸿蒙,端云协同,培养创新型开发者
RY-D1/1电压继电器
208. 实现 Trie (前缀树)
TestNg整合Retry代码
idea的package没有空心
"Learning Cloud Networking with Teacher Tang" - Problem Location - The host is working but the container is not working
原生js 创建表格
R语言怎么绘图(一个r语言完整的命令有什么)
京东校招笔试题+知识点总结
活动速递| Apache Doris 性能优化实战系列直播课程初公开,诚邀您来参加!
Neural Network Study Notes 3 - LSTM Long Short-Term Memory Network
feign远程调用时如何在请求头加入数据
现在报PMP还来得及参加9月的考试吗?分享敏捷全真模拟题
log4js入门
Voltage relay HDY - vac - 1 A / 1-220
淘宝/天猫淘宝评论问答列表接口 API
三个点语法和DOM观察者
Assembly to implement bubble sort
spin lock和mutex使用场景的差异