当前位置:网站首页>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
边栏推荐
- SQL报了一个不常见的错误,让新来的实习生懵了
- Mysql database tourism management system_ Jsp+mysql tourism management system based on SSM [easy to understand]
- Translation (4): matching rules for automatic text completion
- Alchemy (8): parallel development and release
- [VIM] tutorial, common commands, efficient use of vim editor
- Summary of wuenda's machine learning course (11)_ Support vector machine
- 炼金术(2): 为什么要用issue管理软件
- Using two stacks to implement queues [two first in first out is first in first out]
- 互联网的发展为产业的变革和转型提供了新的解决方案
- Arduino uno realizes simple touch switch through direct detection of capacitance
猜你喜欢

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

Safe, fuel-efficient and environment-friendly camel AGM start stop battery is full of charm

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

MySQL企业级参数调优实践分享

Redis主从复制、哨兵模式、集群的概述与搭建

Arduino uno realizes simple touch switch through direct detection of capacitance

How to quote Chinese documents when writing a foreign language?

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

2022 PMP project management examination agile knowledge points (3)

Logging log usage
随机推荐
Deployment and test of crtmp live video server
[AI application] detailed parameters of NVIDIA geforce RTX 1080ti
Sentinel
Transmitting and receiving antenna pattern
Scu| gait switching and target navigation of micro swimming robot through deep reinforcement learning
NoSQL之Redis配置与优化
Alchemy (9): simple but not simple, never-ending test -- always_ run
免费、好用、强大的开源笔记软件综合评测
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation
夏日的晚会
Mise en œuvre du pool de Threads: les sémaphores peuvent également être considérés comme de petites files d'attente
Instructions for vivado FFT IP
Thread pool implementation: semaphores can also be understood as small waiting queues
On charsequence
什么是cookie,以及v-htm的安全性隐患
一个人可以到几家证券公司开户?开户安全吗
CharSequence初探
mysql数据库旅游管理系统_JSP+MySQL基于ssm的旅游管理系统[通俗易懂]
互联网业衍生出来了新的技术,新的模式,新的产业类型
MongoDB-在windows电脑本地安装一个mongodb的数据库