当前位置:网站首页>Explanation of bitwise operators
Explanation of bitwise operators
2022-07-04 22:56:00 【&volume】
Catalog
1.>> Move right
2.<< Move left
Recently, I came into contact with bitwise operators , I think the use of bitwise operators in some problems is very clever , And it is also an introductory Algorithm .
Bit operators have , And , or , Not , Exclusive or , Shift Operators
Bit operators are actually binary operations ,
And (&)
The corresponding bits of two binary operands are the same 1 The result is Only then 1, The rest is 0;
10 Hexadecimal number 15 Binary system 00001111
14 00001110
15&14= 14(00001110)
# include <stdio.h>
int main()
{
printf("%d",14&15);
return 0;
}
or (|)
Only one of the corresponding bits of two binary operands is 1 The result is for 1, The rest is 0
10 Hexadecimal number 15 Binary system 00001111
14 00001110
15|14=15(00001111)
# include <stdio.h>
int main()
{
printf("%d",14|15);
return 0;
}
Not (~)
A binary operand , The corresponding bit is 0, The result bit is 1; The corresponding bit is 1, The result bit is 0;
The reverse result is the original code When storing, it is the complement of storage , To get the complement, you need to get the inverse code first
10 Hexadecimal number 8 Binary system 0000 1000
(~8)=-9(1000 1001)
Reverse the original code :1111 0111
Reverse code after reverse :1000 1000( Except for sign bits The other bits are inverted )
Complement after inversion :1000 1001 Complement code ( Because it is negative, add one ) If it is a positive number, you don't need to add one
# include <stdio.h>
int main()
{
printf("%d",~15);
return 0;
//00001000
//11110111 Reverse original code
//10001000 Take the inverse code
//10001001 Complement code ( Because it is negative, add one )
}
Exclusive or (^)
Both are false , Different is true
10 Hexadecimal number 15 Binary system 00001111
14 00001110
15^14=1(0000001)
# include <stdio.h>
int main()
{
printf("%d",14^15);
return 0;
}
Shift Operators
1.>> Move right
Move all the numbers to the right by the corresponding shift digits in binary form , Move out low ( Abandon ), The sign of the high position , That is, positive number plus zero , Negative numbers make up 1.( The high bit is supplemented by the sign bit ------- Positive number right shift or positive number Negative number right shift or negative number )
grammar : Numbers that need to be shifted number >> The number of shifts
13>>2=3
13 Binary bit of (1101) Move two digits to the right to become 3(11)
nature ( Under integer ): Moving right is equivalent to dividing this number by 2 The number of shifts is rounded down
13/(2^2) Round down to 3
2.<< Move left
Shift the binary of a number to the left at the same time , In the low position 0 Add
5<<1=10
5(101) To the right 1 position (1010)=10、
The nature is the same as above, shift right
yes 5*2^1=10
边栏推荐
- Detailed explanation of heap sort code
- How to send a reliable request before closing the page
- 新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码
- Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
- 微信小程序显示样式知识点总结
- 剑指 Offer 65. 不用加减乘除做加法
- Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
- 企业如何跨越数字化鸿沟?尽在云原生2.0
- 攻防世界 MISC 进阶 glance-50
- 10 schemes to ensure interface data security
猜你喜欢
随机推荐
攻防世界 misc 进阶区 2017_Dating_in_Singapore
Attack and defense world misc master advanced zone 001 normal_ png
String类中的常用方法
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
Photoshop批量给不同的图片添加不同的编号
Feature scaling normalization
Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
云服务器设置ssh密钥登录
SPSS installation and activation tutorial (including network disk link)
Duplicate ADMAS part name
【OpenGL】笔记二十九、抗锯齿(MSAA)
剑指 Offer 65. 不用加减乘除做加法
Detailed explanation of heap sort code
Prosperity is exhausted, things are right and people are wrong: where should personal webmasters go
攻防世界 misc 高手进阶区 a_good_idea
sobel过滤器
In Linux, I call odspcmd to query the database information. How to output silently is to only output values. Don't do this
Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
Analysis of the self increasing and self decreasing of C language function parameters
Sword finger offer 67 Convert a string to an integer