当前位置:网站首页>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
边栏推荐
- Pytorch Foundation (1)
- Alchemy (1): identify prototype, demo and MVP in project development
- Oracle数据库的启停
- 积分体系和营销活动结合在一起有哪些玩法
- Summary of wuenda's machine learning course (11)_ Support vector machine
- Msp430f5529 MCU reads gy-906 infrared temperature sensor
- Deployment and test of crtmp live video server
- [AI application] detailed parameters of NVIDIA Tesla v100-pcie-32gb
- Solve the cross domain problem of the new version of chrome: Cookie loss and samesite attribute problem "recommended collection"
- 夏日的晚会
猜你喜欢

MongoDB-在windows电脑本地安装一个mongodb的数据库

表单form 和 表单元素(input、select、textarea等)
![计数质数[枚举 -> 空间换时间]](/img/11/c52e1dfce8e35307c848d12ccc6454.png)
计数质数[枚举 -> 空间换时间]

夏日的晚会

Flutter series: Transformers in flutter

flutter系列之:flutter中的变形金刚Transform

Msp430f5529 MCU reads gy-906 infrared temperature sensor

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

Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance

本地可视化工具连接阿里云centOS服务器的redis
随机推荐
CRTMP视频直播服务器部署及测试
Alchemy (3): how to do a good job in interfacing a business process
MySQL分表查询之Merge存储引擎实现
Arduino UNO通过电容的直接检测实现简易触摸开关
单片机之IIC通信协议「建议收藏」
Webserver flow chart -- understand the calling relationship between webserver modules
internship:业务流程初识
Using two stacks to implement queues [two first in first out is first in first out]
Comprehensive evaluation of free, easy-to-use and powerful open source note taking software
Technical debt wall: a way to make technical debt visible and negotiable
数仓的字符截取三胞胎:substrb、substr、substring
夏日的晚会
用两个栈实现队列[两次先进后出便是先进先出]
Alchemy (7): how to solve problems? Only reconstruction
炼金术(3): 怎样做好1个业务流程的接口对接
吴恩达《机器学习》课程总结(11)_支持向量机
[PCL self study: Segmentation3] PCL based point cloud segmentation: region growth segmentation
证券注册账户安全吗,会有风险吗?
吴恩达《机器学习》课程总结(14)_降维
What are the ways to combine the points system with marketing activities