371. Sum of Two Integers(位操作)

2016-09-06  本文已影响0人  Asian_Road

Calculate the sum of two integers a and b, but you are not allowed to use the operator+and-


1.位操作符

位操作只用于整形数据

2.算法
class Solution{
public:
  int add (int a,  int b)  { 
    while(a!=0 && b!=0){ 
       int temp = a; 
        a = temp & b; 
        b = b ^ temp; 
        a = a << 1; 
    }  
    return a == 0?b:a; 
  } 
};
上一篇 下一篇

猜你喜欢

热点阅读