当前位置:网站首页>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
边栏推荐
- Logo special training camp Section IV importance of font design
- Google Earth engine (GEE) -- take modis/006/mcd19a2 as an example to batch download the daily mean, maximum, minimum, standard deviation, statistical analysis of variance and CSV download of daily AOD
- 共创软硬件协同生态:Graphcore IPU与百度飞桨的“联合提交”亮相MLPerf
- Redis: redis configuration file related configuration and redis persistence
- Co create a collaborative ecosystem of software and hardware: the "Joint submission" of graphcore IPU and Baidu PaddlePaddle appeared in mlperf
- MySQL Architecture - logical architecture
- Redis入门完整教程:事务与Lua
- 质量体系建设之路的分分合合
- Close system call analysis - Performance Optimization
- Create Ca and issue certificate through go language
猜你喜欢

Sobel filter

NFT insider 64: e-commerce giant eBay submitted an NFT related trademark application, and KPMG will invest $30million in Web3 and metauniverse

集群的概述与定义,一看就会

NFT Insider #64:电商巨头eBay提交NFT相关商标申请,毕马威将在Web3和元宇宙中投入3000万美元

Redis入门完整教程:Pipeline

PMO: compare the sample efficiency of 25 molecular optimization methods

攻防世界 MISC 高手进阶区 001 normal_png

【室友用一局王者荣耀的时间学会了用BI报表数据处理】

Persistence mechanism of redis

Attack and defense world misc advanced area can_ has_ stdio?
随机推荐
华泰证券是国家认可的券商吗?开户安不安全?
Redis入门完整教程:列表讲解
Attack and defense world misc advanced zone 2017_ Dating_ in_ Singapore
Attack and Defense World MISC Advanced Area Erik baleog and Olaf
【剑指Offer】6-10题
Redis的持久化机制
On-off and on-off of quality system construction
9 - 类
Sword finger offer 68 - I. nearest common ancestor of binary search tree
攻防世界 MISC 进阶区 Ditf
Google collab trample pit
Advanced area a of attack and defense world misc Masters_ good_ idea
醒悟的日子,我是怎么一步一步走向软件测试的道路
Redis入门完整教程:Bitmaps
Redis入门完整教程:API的理解和使用
MySQL Architecture - logical architecture
SPSS installation and activation tutorial (including network disk link)
Redis入门完整教程:客户端通信协议
[cooking record] - stir fried 1000 pieces of green pepper
Async await used in map