当前位置:网站首页>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
其他的操作类似.
如果对嵌入式技术感兴趣的,欢迎关注微信公众号“嵌入式之入坑笔记”,一起学习讨论啊!
边栏推荐
猜你喜欢

The battle-hardened programmer was also deceived by a fake programmer from a certain fish. The trust between programmers should be the highest, and he alone destroyed this sense of trust

【C和指针第七章】可变参数列表

鸿湖万联扬帆富设备开发板正式合入OpenHarmony主干

Pytorch中 nn.Transformer的使用详解与Transformer的黑盒讲解
![【 HMS core 】 【 Analytics Kit] [FAQ] how to solve the payment amount in huawei pay analysis shows zero problem?](/img/f3/b9256fc04d1c9e15c74d2fc14db0fb.png)
【 HMS core 】 【 Analytics Kit] [FAQ] how to solve the payment amount in huawei pay analysis shows zero problem?

Typroa alternative tool marktext

Beyond Stream Processing !第四届实时计算 Flink 挑战赛启动,49 万奖金等你来拿!

鸿湖万联扬帆富设备开发板正式合入OpenHarmony主干

Still using Swagger?I recommend this interface management artifact with zero code intrusion

Selected System Design | Design of CAN Bus Controller Based on FPGA (with Code)
随机推荐
IP池设计思考(面试点)[通俗易懂]
【梦想起航】
从数据流中快速查找中位数
RY-D1/1 Voltage Relay
RY-D1/1电压继电器
汇编实现冒泡排序
Easy gene: human tRNA gene loci showed age-related high DNA methylation | research articles
Oracle中SQL语言和分页rownum分析
【ASP.NET Core】选项类的依赖注入
Neural Network Study Notes 4 - Autoencoder (including sparse, stacked) (updated)
wsl操作
The configuration process and related syntax of writing markdown format notes in vscode
Neural Network Study Notes 3 - LSTM Long Short-Term Memory Network
[ASP.NET Core] Dependency Injection for Option Classes
STM32F1 reads MLX90632 non-contact infrared temperature sensor
美团内推+校招笔试题+知识点总结
实现web实时消息推送的7种方案
Voltage relay h2d SRMUVS - 100 vac - 2
log4j Logger简介说明
vscode中写markdown格式笔记的配置过程和相关语法