当前位置:网站首页>Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
Sword finger offer 65 Add without adding, subtracting, multiplying, dividing
2022-06-28 00:22:00 【InfoQ】
️ The finger of the sword Offer 65. Do not add, subtract, multiply or divide ️
Topic details
Input : a = 1, b = 1
Output : 2
Input : a = 5, b = 6
Output : 11
- a, b Both can be negative or 0.
- The result won't spill 32 An integer .
Their thinking
- [x] Bitwise AND & : Binocular operator , Take and... For each , All for 1 Then for 1, Otherwise 0.
- [x] Bitwise XOR ^ : Binocular operator , Take XOR for each , If the corresponding two numbers are different, they are 1, Otherwise 0.
- [x] Move left << : Binocular operator ,a << b , It means that you will a The binary bit of is shifted left b position , Rightmost complement 0.
- [x] Characteristics of additive carry : For binary , The condition of carry is that the binary bits corresponding to two numbers are 1.


- take a And b Press and and move left one bit , namely (a & b) << 1, You might as well save this result in a variable tmp, If the value is 0, It means that there is no carry of two numbers .
- take a And b Perform non carry addition , namely a ^ b, You might as well assign this result to a, Take the above tmp The value assigned to b.
- here b The value of is tmp, Judge b Is it 0, if b Not for 0, Carry required , Repeat the above steps , Carry the number b Non carry addition to the value of the previous non carry addition , if b by 0, There is no need to carry , The addition process ends , The end result is a Value .

Source code
class Solution {
public int getSum(int a, int b) {
// The XOR of two numbers is equivalent to adding without carry , The bitwise sum operation of two numbers can get the number that needs to be carried , Move this number to the left and add XOR , Until the number is 0, The addition is done
while (b != 0) {
int tmp = (a & b) << 1;
a ^= b;
b = tmp;
}
return a;
}
}
int getSum(int a, int b){
while (b) {
int tmp = (unsigned int)(a & b) << 1;//C/C++, In the case of negative numbers , Overflow exists for the left shift of signed number , First turn to unsigned and then move left to protect overflow
a ^= b;
b = tmp;
}
return a;
}
class Solution {
public:
int getSum(int a, int b) {
while (b) {
int tmp = (unsigned int)(a & b) << 1;
a ^= b;
b = tmp;
}
return a;
}
};
summary
边栏推荐
- Recyclerview implements grouping effects in a variety of ways
- 用两个栈实现队列[两次先进后出便是先进先出]
- 软件工程作业设计(1): [个人项目] 实现一个日志查看页面
- Local visualization tool connects to redis of Alibaba cloud CentOS server
- How to quote Chinese documents when writing a foreign language?
- RNA SEQ introduction practice (I): upstream data download, format conversion and quality control cleaning
- How to select documents for literature review? For example, I can't finish reading more than 200 search results. How to select documents
- Mise en œuvre du pool de Threads: les sémaphores peuvent également être considérés comme de petites files d'attente
- ASP. Net warehouse purchase, sales and inventory ERP management system source code ERP applet source code
- Build an open source and beautiful database monitoring system -lepus
猜你喜欢

MySQL enterprise parameter tuning practice sharing

Webserver flow chart -- understand the calling relationship between webserver modules

Character interception triplets of data warehouse: substrb, substr, substring

Alchemy (1): identify prototype, demo and MVP in project development

吴恩达《机器学习》课程总结(13)_聚类

安全省油环保 骆驼AGM启停电池魅力十足

The limits of Technology (11): interesting programming

logging日志的使用
![[digital ic/fpga] detect the position of the last matching sequence](/img/67/a1b575aa9b63892ed585d39e615c58.png)
[digital ic/fpga] detect the position of the last matching sequence

Arduino uno realizes simple touch switch through direct detection of capacitance
随机推荐
VirtualBox extended dynamic disk size pit
Code neatness -- format
The Internet industry has derived new technologies, new models and new types of industries
線程池實現:信號量也可以理解成小等待隊列
internship:业务流程初识
炼金术(9): 简约而不简单,永不停歇的测试 -- always_run
[paper reading | deep reading] sdne:structural deep network embedding
互联网业衍生出来了新的技术,新的模式,新的产业类型
Sécurité, économie de carburant et protection de l'environnement chameau
The limits of Technology (11): interesting programming
Pytorch Foundation (1)
2022 PMP project management examination agile knowledge points (3)
Golang uses Mongo driver operation - query (Advanced)
It supports deleting and updating the priority queue of any node
Local visualization tool connects to redis of Alibaba cloud CentOS server
【无标题】
Chenyun pytorch learning notes_ Build RESNET with 50 lines of code
Sell notes | brief introduction to video text pre training
[idea] idea formatting code skills
Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance