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

 

posted @ 2015-11-26 19:33  ZH奶酪  阅读(186)  评论(0编辑  收藏  举报