剑指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/

剑指offer 面试题65. 不用加减乘除做加法 [简单]_第1张图片

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

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;
    }
};

 

你可能感兴趣的:(剑指offer,LeetCode)