JZ48 不用加减乘除做加法

不用加减乘除做加法

https://www.nowcoder.com/practice/59ac416b4b944300b617d4f7f111b215?tpId=13&&tqId=11201&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking

牛逼

public class Solution {
    public int Add(int num1,int num2) {
        while(num2 != 0) { // 当进位为 0 时跳出
            int c = (num1 & num2) << 1;  // 先算当前进位  c = 进位
            num1 ^= num2; // num1 = 非进位和  再算num1 num2 异或
            num2 = c; // num2 = 进位
        }
        return num1;
    }
}
全部评论

相关推荐

点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务