1. 程式人生 > >371. Sum of Two Integers也許是最簡潔易懂的做法了

371. Sum of Two Integers也許是最簡潔易懂的做法了

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

Example:
Given a = 1 and b = 2, return 3.

實現:

class Solution {
public:
    int getSum(int a, int b) {
        while (b){
            int x = a ^ b, y = (a & b) << 1;
            a = x; b = y;
        }
        return a;
    }
};