当前位置:网站首页>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
边栏推荐
- Attack and Defense World MISC Advanced Area Erik baleog and Olaf
- Google Earth Engine(GEE)——基于 MCD64A1 的 GlobFire 日常火灾数据集
- 环境加密技术解析
- Redis: redis configuration file related configuration and redis persistence
- Redis入門完整教程:Pipeline
- Redis入门完整教程:事务与Lua
- 9 - class
- Locust performance test - environment construction and use
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- 【lua】int64的支持
猜你喜欢

MySQL Architecture - logical architecture

Attack and defense world misc advanced area can_ has_ stdio?

Attack and Defense World MISC Advanced Area Erik baleog and Olaf

PMO: compare the sample efficiency of 25 molecular optimization methods

常用技术指标之一文读懂BOLL布林线指标

Logo special training camp Section IV importance of font design

堆排序代码详解

Persistence mechanism of redis

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

Redis: redis configuration file related configuration and redis persistence
随机推荐
【机器学习】手写数字识别
The difference between Max and greatest in SQL
质量体系建设之路的分分合合
Now MySQL cdc2.1 is parsing the datetime class with a value of 0000-00-00 00:00:00
String类中的常用方法
About stack area, heap area, global area, text constant area and program code area
Summary of index operations in mongodb
繁華落盡、物是人非:個人站長該何去何從
Breakpoint debugging under vs2019 c release
Logo special training camp section II collocation relationship between words and graphics
Create Ca and issue certificate through go language
Sword finger offer 67 Convert a string to an integer
Tla+ introductory tutorial (1): introduction to formal methods
记录:关于Win10系统中Microsoft Edge上的网页如何滚动截屏?
Record: how to scroll screenshots of web pages on Microsoft edge in win10 system?
leetcode 72. Edit distance edit distance (medium)
攻防世界 MISC 进阶 glance-50
Install the gold warehouse database of NPC
Redis: redis configuration file related configuration and redis persistence
云服务器设置ssh密钥登录