剑指offer 面试题65. 不用加减乘除做加法 [简单]

https://leetcode-cn.com/problems/bu-yong-jia-jian-cheng-chu-zuo-jia-fa-lcof/solution/mian-shi-ti-65-bu-yong-jia-jian-cheng-chu-zuo-ji-7/

位运算搞不明白,就记住吧

class Solution {
public:
    int add(int a, int b) {
        int c=0;
        while(b){
            c=(unsigned int)(a&b)<<1;
            a^=b;
            b=c;
        }
        return a;
    }
};

猜你喜欢

转载自blog.csdn.net/qq_41041762/article/details/105898020