不用+,-,*,/写一个计算加法的函数

class Solution {
public:
    int add(int a, int b) {
       int c;
       while(b!=0){
           c=(unsigned int)(a&b)<<1;//进位
           a^=b;//a+b
           b=c;//进位赋给b,直到没有进位,因为a加上进位可能还产生进位
       }
       return a;
    }
};
posted @ 2021-03-29 20:14  tao10203  阅读(41)  评论(0)    收藏  举报