当前位置:网站首页>剑指 Offer 65. 不用加减乘除做加法
剑指 Offer 65. 不用加减乘除做加法
2022-06-27 21:58:00 【InfoQ】
️剑指 Offer 65. 不用加减乘除做加法️
题目详情
输入: a = 1, b = 1
输出: 2
输入: a = 5, b = 6
输出: 11
- a, b 均可能是负数或 0。
- 结果不会溢出 32 位整数。
解题思路
- [x] 按位与 & :双目运算符,对每位取与,都为1则为1,否则为0。
- [x] 按位异或 ^ :双目运算符,对每位取异或,对应位两数不相同为1,否则为0。
- [x] 左移 << :双目运算符,a << b ,表示将a的二进制位左移b位,最右边补0。
- [x] 加法进位的特点:对于二进制,发生进位的条件是两个数所对应的二进制位均为1。


- 将a与b按位与并左移一位,即(a & b) << 1,不妨将此结果存至变量tmp,若此数值为0,就表示两数未发生进位。
- 将a与b进行非进位加法,即a ^ b,不妨将此结果赋值给a,将上面所得tmp值赋值给b。
- 此时b的值为tmp,判断b是否为0,若b不为0,需要进位,重复上述步骤,将进位数b与前一次非进位加法的值非进位相加,若b为0,不需要进位,加法过程结束,最终结果即a的值。

源代码
class Solution {
public int getSum(int a, int b) {
//两数异或相当于不进位相加,两数按位与运算能够得到需要进位的数,将此数左移再异或相加,直到该数为0,加法计算完毕
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++,负数情况,对有符号数左移存在溢出,先转无符号再左移对溢出情况保护处理
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;
}
};
总结
边栏推荐
- Smart wind power | Tupu software digital twin wind turbine equipment, 3D visual intelligent operation and maintenance
- Feign implements path escape through custom annotations
- Detailed explanation of MATLAB axis coordinate axis related settings
- How to use raspberry pie (and all kinds of pies)
- Solve the cross domain problem of the new version of chrome: Cookie loss and samesite attribute problem "recommended collection"
- How to quote Chinese documents when writing a foreign language?
- MongoDB-在windows电脑本地安装一个mongodb的数据库
- 积分体系和营销活动结合在一起有哪些玩法
- Chapter 2 integrated mp
- SCU|通过深度强化学习进行微型游泳机器人的步态切换和目标导航
猜你喜欢

MySQL enterprise parameter tuning practice sharing

Matlb| optimal configuration of microgrid in distribution system based on complex network
![Count prime [enumeration - > space for time]](/img/11/c52e1dfce8e35307c848d12ccc6454.png)
Count prime [enumeration - > space for time]

安全省油環保 駱駝AGM啟停電池魅力十足
![用两个栈实现队列[两次先进后出便是先进先出]](/img/de/07297816f1a44d41389bb45d012c80.png)
用两个栈实现队列[两次先进后出便是先进先出]

An analysis of C language functions

Sécurité, économie de carburant et protection de l'environnement chameau

现代编程语言:zig
![[microservices sentinel] sentinel data persistence](/img/9f/2767945db99761bb35e2bb5434b44d.png)
[microservices sentinel] sentinel data persistence

MongoDB-在windows电脑本地安装一个mongodb的数据库
随机推荐
What are the ways to combine the points system with marketing activities
软件工程作业设计(1): [个人项目] 实现一个日志查看页面
Pat class B 1013
MySQL read / write separation configuration
吴恩达《机器学习》课程总结(14)_降维
炼金术(4): 程序员的心智模型
Sentinel
Grab those duplicate genes
Oracle数据库的启停
炼金术(6): 可迭代的模型和用例
Hcip/hcie Routing & Switching / datacom Reference Dictionary Series (19) comprehensive summary of PKI knowledge points (public key infrastructure)
Flutter series: Transformers in flutter
股市小白在网上股票开户安全吗?
ValidateRequest=”false” 是做什么的「建议收藏」
At the beginning of reading English literature, I would like to ask you how you should read it in the first place?
Code neatness -- function
现代编程语言:zig
RNA-seq入门实战(一):上游数据下载、格式转化和质控清洗
[AI application] detailed parameters of NVIDIA geforce RTX 3060
吴恩达《机器学习》课程总结(11)_支持向量机