当前位置:网站首页>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
边栏推荐
猜你喜欢

Redis入门完整教程:客户端通信协议

Attack and defense world misc master advanced zone 001 normal_ png

Attack and defense world misc advanced area can_ has_ stdio?

新版判断PC和手机端代码,手机端跳转手机端,PC跳转PC端最新有效代码

Advanced area of attack and defense world misc 3-11

攻防世界 MISC 进阶区 Ditf

Advanced area a of attack and defense world misc Masters_ good_ idea
页面关闭前,如何发送一个可靠请求

Unity-VScode-Emmylua配置报错解决

Lost in the lock world of MySQL
随机推荐
华泰证券是国家认可的券商吗?开户安不安全?
Analysis of the self increasing and self decreasing of C language function parameters
Sobel filter
微信公众号解决从自定义菜单进入的缓存问题
【lua】int64的支持
SQL中MAX与GREATEST的区别
该如何去选择证券公司,手机上开户安不安全
Google Earth engine (GEE) - globfire daily fire data set based on mcd64a1
Redis入门完整教程:客户端通信协议
Google Earth engine (GEE) - tasks upgrade enables run all to download all images in task types with one click
Redis démarrer le tutoriel complet: Pipeline
How can enterprises cross the digital divide? In cloud native 2.0
It is said that software testing is very simple, but why are there so many dissuasions?
Advanced area a of attack and defense world misc Masters_ good_ idea
Redis入门完整教程:有序集合详解
[try to hack] wide byte injection
[cooking record] - stir fried 1000 pieces of green pepper
攻防世界 MISC 进阶区 hit-the-core
常用技术指标之一文读懂BOLL布林线指标
NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse