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

ABP学习资源整理

Pytorch中 nn.Transformer的使用详解与Transformer的黑盒讲解

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

第3章 信息收集

360 released a future-oriented EDR to protect the security of government and enterprise user terminals in an all-round way

神经网络学习笔记3——LSTM长短期记忆网络

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

电压继电器HDY-A/1-220VAC-1

RY-D1/1电压继电器

Neural Network Study Notes 4 - Autoencoder (including sparse, stacked) (updated)
随机推荐
eric6教程(电脑的配置基本知识)
RY-D1/1 Voltage Relay
类和对象—6个默认成员函数
向上管理读书笔记
NLP领域的最新研究进展
Unity 锁定相机第二弹
定制.NET 6.0的依赖注入
美团内推+校招笔试题+知识点总结
单片机开发之LCD1602显示实验
从数据流中快速查找中位数
Hu-cang integrated e-commerce project (1): project background and structure introduction
久经沙场的程序员居然也被某鱼的假程序员骗了,程序员之间的信任应该是最高的,他一个人毁了这种信任感
现在报PMP还来得及参加9月的考试吗?分享敏捷全真模拟题
How to add data to the request header when feign is called remotely
jmeter接口压力测试(一)
VLAN实验
Voltage relay h2d SRMUVS - 100 vac - 2
feign远程调用时如何在请求头加入数据
基于.NetCore开发博客项目 StarBlog - (16) 一些新功能 (监控/统计/配置/初始化)
Selected System Design | Design of CAN Bus Controller Based on FPGA (with Code)