当前位置:网站首页>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
其他的操作类似.
如果对嵌入式技术感兴趣的,欢迎关注微信公众号“嵌入式之入坑笔记”,一起学习讨论啊!
边栏推荐
- I built another wheel: GrpcGateway
- PL5920 SOT-23-6 21V、2A、600KHz同步降压DC/DC转换器
- Detailed explanation of @RequestBody and @ResponseBody
- log4j Logger简介说明
- oracle 导出dmp文件类型为“故障转储文件”
- 【ASP.NET Core】选项类的依赖注入
- STM32F1读取MLX90632非接触式红外温度传感器
- 208. 实现 Trie (前缀树)
- 分布式限流 redission RRateLimiter 的使用及原理
- 360发布面向未来的EDR,全方位守护政企用户终端安全
猜你喜欢
随机推荐
360闷声干大事获赞无数,数字安全如何保障?还得看企业安全云
类和对象—6个默认成员函数
获取1688app上原数据 API
Assembly to implement bubble sort
HJY-F931A/YJ three-phase voltage relay
Swift common extension classes and simple encapsulation
Is it too late to apply for PMP now to take the September exam?Share agile full-true mock questions
Telerik2022 R2,有效的自动化测试
NLP领域的最新研究进展
Database transactions, JDBC operations and data types
Log4j有哪几种日志级别呢?
Beyond Stream Processing !第四届实时计算 Flink 挑战赛启动,49 万奖金等你来拿!
Voltage relay h2d SRMUVS - 100 vac - 2
我又造了个轮子:GrpcGateway
Unity 锁定相机第二弹
RY-D1/1 Voltage Relay
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
关于verilog的时延研究
物联网技术概论:第6章
单片机开发之LCD1602显示实验









