当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
Zabbix: PHP option“date.timezone” Fail
抽象类和接口 基本知识点复习
数据源,分层开发以及jsp标签总结及相关代码
adb常用命令
在命令行或者pycharm安装库时出现:ModuleNotFoundError: No module named ‘pip‘ 解决方法
软件测试之WEB自动化
【软件测试】基础篇
CUDA programming based on Visual Studio 2015 (1): basic configuration
test2
WEB自动化之多窗口操作、切换frame、弹窗处理
makefile——杂项
smart_rtmpd 的 VOD 接口使用说明
golang中使用泛型
LAMP环境 源码编译安装(Apache 2.4.52 +mysql 8.0.28+php 8.1.3)
ADB常用命令--测试人员必备
【软件测试】进阶篇
数据库性能优化的误区!
DOM —— 页面的渲染流程
【软件测试】测试基础讲解
这几年让你大呼惊人的AI应用,都离不开这项技术