当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
【软件测试】基础篇
炒鸡好用的音乐平台(插件)
【web渗透】文件包含漏洞入门级超详细讲解
支付系列文章:PCI合规能力建设
假的服务器日志(给history内容增加ip、用户等内容)
grub 命令使用
test2
Principles of permutation entropy, fuzzy entropy, approximate entropy, sample entropy and approximate entropy implemented by MATLAB
webrtc 数据接收流程图解
DOM —— 元素盒子模型
GC垃圾收集器G1
ks.cfg 怎么读取光盘 (cdrom) 上的文件并执行对应的脚本
小知识点系列:StringUtil.isEmpty()与StringUtil.isBlank()的区别
web渗透之文件上传漏洞
test3
JVM常量池详解
CUDA programming based on Visual Studio 2015 (1): basic configuration
数据源,分层开发以及jsp标签总结及相关代码
CSV file with the data read and write 】 【 XLS/XLSX file
网络运维系列:GoDaddy Shell DDNS配置