371 Sum of Two Integers 两整数之和

不使用运算符 + 和-,计算两整数a 、b之和。
示例:
若 a = 1 ,b = 2,返回 3。

详见:https://leetcode.com/problems/sum-of-two-integers/description/

C++:

class Solution {
public:
    int getSum(int a, int b) {
        while(b)
        {
            int tmp=a^b;
            b=(a&b)<<1;
            a=tmp;
        }
        return a;
    }
};

 参考:https://www.cnblogs.com/grandyang/p/5631814.html

posted on 2018-04-15 16:26  lina2014  阅读(159)  评论(0编辑  收藏  举报

导航