leetcode 371. Sum of Two Integers

异或其实是无进位加法,与取进位。

    int getSum(int a, int b) {
        int ret = a ^ b;
        int carry = a & b;
        while (carry) {
            carry = carry << 1;
            int temp = carry;
            carry = carry & ret;
            ret = ret ^ temp;
        }
        return ret;
    }

 

posted on 2018-02-09 18:15  willaty  阅读(78)  评论(0编辑  收藏  举报

导航