剑指OFFER----面试题65. 不用加减乘除做加法

链接:https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/

代码

class Solution {
public:
    int add(int a, int b) {  
        int sum, carry;
        while (b) {
            sum = a ^ b;
            carry = (unsigned int)(a & b) << 1;
            a = sum;
            b = carry;
        }
        return a;
    }
};
posted @ 2020-03-15 13:03  景云ⁿ  阅读(84)  评论(0编辑  收藏  举报