LintCode: A + B Problem

C++

 1 class Solution {
 2 public:
 3     /*
 4      * @param a: The first integer
 5      * @param b: The second integer
 6      * @return: The sum of a and b
 7      */
 8     int aplusb(int a, int b) {
 9         // write your code here, try to do it without arithmetic operators.
10         int sum = a^b;
11         int carry = (a&b) << 1;
12         int tmp;
13         while (carry) {
14             tmp = sum;
15             sum = tmp^carry;
16             carry = (tmp&carry) << 1;
17         }
18         return sum;
19     }
20 };
原文地址:https://www.cnblogs.com/CheeseZH/p/4998708.html