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.

此题讲解全部都在bit manipulation里面:

public class Solution {

    public int getSum(int a, int b) {

        if(b==0) return a;

        return getSum(a^b,(a&b)<<1);

    }

}

posted @ 2017-01-31 05:25  CodesKiller  阅读(96)  评论(0编辑  收藏  举报