当前位置:网站首页>Preparing for the Blue Bridge Cup -- bit operation
Preparing for the Blue Bridge Cup -- bit operation
2022-07-01 09:10:00 【Ceylan_】
Catalog
The finger of the sword Offer 64. seek 1+2+…+n
What is bit operation
All the numbers in the program are stored in the computer memory in binary form . Bit operation is to operate the binary bit of integer in memory directly , Bit operation is very efficient , Try to use bit operation in the program , This will greatly improve the performance of the program .
An operator
And operation &
and Operations are usually used for binary bit operations , For example, a number & 1 The result is to take the last bit of the binary . This can be used to determine the parity of an integer , The last bit of binary is 0 Indicates that the number is even , The last is 1 Indicates that the number is odd .
Two numbers with the same digit All for 1, Then for 1; If you have any One is not for 1, Then for 0.
00101 & 11100=00100
Or operations |
or Operations are usually used for unconditional assignments on binary specific positions , For example, a number | 1 The result is to force the last bit of the binary into 1. If you need to change the last bit of the binary to 0, For this number | 1 After that, just subtract one , Its practical significance is to force this number to the nearest even number .
Same bit as long as One for 1 That is to say 1.
00101 | 11100=11101
Exclusive or operation ^
The sign of XOR is ^. Bitwise exclusive or operation , Perform a logical bitwise XOR operation on each bit of the bitwise or binary number of the equal length binary pattern , The result of the operation is that if a bit is different, the bit is 1, Otherwise, this bit is 0.
In a nutshell Same as 0, Different for 1.
00101 ^ 11100=11001
Reverse operation ~
The definition of negation operation is to take 0 and 1 All reversed .
~ 00101=11010
Left shift operation <<
a << b It means to put a Shift left after binary b position ( Add after b individual 0). for example 100 The binary of is 1100100, and 110010000 To convert to decimal is 400, that 100 << 2 = 400. It can be seen that ,a << b The value of is actually a multiply 2 Of b Power , Because add a... After the binary number 0 It's equivalent to multiplying this number by 2.
It is commonly believed a << 1 Than a * 2 faster , Because the former is a lower level operation . So the program multiplies by 2 Please try to shift one bit to the left instead of .
Right shift operation >>
and << be similar ,a >> b Indicates binary shift right b position ( Remove the end b position ), amount to a Divide 2 Of b Power ( integer ). The same example as above , that 4002 = 100
priority
Priority | Operator |
1 | ~( According to the not ) |
2 | <<( Move left ) >>( Move right ) |
3 | &( Bitwise AND ) |
4 | ^( Bitwise XOR ) |
5 | |( Press bit or ) |
Example
371. Sum of two integers

class Solution {
public:
int getSum(int a, int b) {
while (b != 0) {
unsigned int c = (unsigned int)(a & b) << 1;
a = a ^ b;
b = c;
}
return a;
}
};
The finger of the sword Offer 64. seek 1+2+…+n

int quickMulti(int A, int B) {
int ans = 0;
for ( ; B; B >>= 1) {
if (B & 1) {
ans += A;
}
A <<= 1;
}
return ans;
}
link
Prepare for the Blue Bridge Cup ———— Operator ^
Prepare for the Blue Bridge Cup ———— The pointer
Prepare for the Blue Bridge Cup ———— Dichotomy search
Prepare for the Blue Bridge Cup ———— A collection of commonly used string functions
I am not talented , If there is a mistake , Welcome to the comments section . If it helps you, please give the thumbs-up , Collection , Focus on Oh !

One sentence per day
Efforts and gains , It's all my own , It has nothing to do with others . The greatest sense of achievement , Is always moving in the direction you want
边栏推荐
- Pain points and solutions of equipment management in large factories
- 钓鱼识别app
- Shell script -if else statement
- 【pytorch】transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
- 2.3 【kaggle数据集 - dog breed 举例】数据预处理、重写Dataset、DataLoader读取数据
- What are the differences between the architecture a, R and m of arm V7, and in which fields are they applied?
- 【pytorch】softmax函数
- 【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
- Win7 pyinstaller reports an error DLL load failed while importing after packaging exe_ Socket: parameter error
- Shell脚本-字符串
猜你喜欢
随机推荐
【pytorch】softmax函数
Naoqi robot summary 28
[ESP nanny level tutorial preview] crazy node JS server - Case: esp8266 + DS18B20 temperature sensor +nodejs local service + MySQL database
Yidian Yidong helps enterprises to efficiently manage equipment and improve equipment utilization
Mysql 优化
Input标签的type设置为number,去掉上下箭头
Installing Oracle EE
【pytorch】nn.CrossEntropyLoss() 与 nn.NLLLoss()
【ESP 保姆级教程 预告】疯狂Node.js服务器篇 ——案例:ESP8266 + DS18B20温度传感器 +NodeJs本地服务+ MySQL数据库
Record a redis timeout
3. Detailed explanation of Modbus communication protocol
Redis source code learning (29), compressed list learning, ziplist C (II)
Embedded Engineer Interview frequently asked questions
Bird recognition app
Shell script -while loop explanation
An overview of the design of royalties and service fees of mainstream NFT market platforms
Pain points and solutions of equipment management in large factories
NiO zero copy
Ape anthropology topic 20 (the topic will be updated from time to time)
VSYNC+三重缓存机制+Choreographer



![[interview brush 101] linked list](/img/52/d159bc66c0dbc44c1282a96cf6b2fd.png)



