当前位置:网站首页>Explain in detail how the bit manipulation operators in C language can be used?
Explain in detail how the bit manipulation operators in C language can be used?
2022-08-02 16:35:00 【Zhi Zhao】
Directory
Foreword
I recently encountered bit manipulation operators in C language at work, and found myself ambiguous about the content of this piece, so I plan to write a blog post to record this knowledge point. Today is the last day of March, so addA class to make up for the regret of not writing a blog post in March.Bit operations are commonly used in embedded development and can be used for some special operations.Let's go to the topic and talk about the six bitwise operators commonly used in the C language.
Introduction to bitwise operators
C language provides 6 bit operation operators, these operators can only be used for char, short, int, long types.
The symbols in the following single quotation marks are the 6 bit manipulation operators mentioned.
- '&': bitwise AND operation, and "and" operation by binary bit;
- '|': bitwise OR operation, "or" operation by binary bit;
- '^': bitwise XOR operation, "XOR" operation by binary bit;
- '~': bitwise inversion operation, "inversion" operation by binary bit;
- ‘<<’: Binary left shift operator, which shifts all the binary bits of an operand to the left by several bits, discards the binary bits on the left, and adds 0 to the right;
- '>>': Binary right shift operator, which shifts all the binary bits of an operand to the right by a few bits, positive numbers are left with 0, negative numbers are left with 1, and the right side is discarded.
Application of bitwise operators
General Cases
Suppose if A = 60, and B = 13, now in binary format, they look like this:
A = 0011 1100
B = 0000 1101
Operates on A and B bitwise operators:
A&B = 0000 1100
Description: The values on the corresponding bits in A and B are ANDed, similar to the && operator,
0&0=0;
0&1=0;
1&0=0;
1&1=1;
A|B = 0011 1101
Explanation: The values on the corresponding bits in A and B are ORed, similar to the | | operator,
0|0=0;
0|1=1;
1|0=1;
1|1=1;
A^B = 0011 0001
Explanation: The values on the corresponding bits in A and B are XORed, the same is 0, the difference is 1, 0^0=0;
0^1=1;
1^0=1;
1^1=0;
~A = 1100 0011
Description: Invert the value of each bit in A, the inversion of 1 is 0, and the inversion of 0 is 1.
A << 2 equals 1111 0000, which is 240 in decimal.
A >> 2 equals 0000 1111, which is 15 in decimal.
Special usage
1) The bitwise AND operator & is often used to mask certain binary bits, such as:
x = 5; (0101)
y = 10; (1010)
z = x & y
At this point z is equal to 0 (0000).
2) The bitwise OR operator | is often used to set some binary bits to 1, such as:
x = 5; (0101)
y = 10; (1010)
z = x| y
At this point z is equal to 15 (1111).
3) The bitwise exclusive OR operator ^ can set the bit to 1 when the corresponding bits of the two numbers are different, and set the bit to 0 when they are the same.
If there are better examples in the future, I can put them up.
边栏推荐
猜你喜欢
随机推荐
常见(MySQL)面试题(含答案)
abstract和接口的基础知识
test3
adb常用命令
关于导出聊天记录这件事……
CPU缓存一致性协议MESI
Scala的模式匹配与样例类
小知识系列:Fork之后如何与原仓库分支同步
CUDA programming based on Visual Studio 2015 (1): basic configuration
Oauth2.0 authentication server construction
Scala的安装和IDEA的使用(初入茅庐)
WeTest----如何查看Wetest生成测试报告?
H3C 交换机配置端口组、DHCP、DHCP中继、管理用户
自动化之参数化
2021年度总结——收获圆满的一年
(三)文件操作之一——文件IO
Principles of permutation entropy, fuzzy entropy, approximate entropy, sample entropy and approximate entropy implemented by MATLAB
Mysql索引底层数据结构
Mysql锁机制与事务隔离
makefile——rule概览