A + B Problem

Write a function that add two numbers A and B. You should not use + or any arithmetic operators.

分析:

典型的Bit Operation.

 1 class Solution {
 2     /*
 3      * param a: The first integer
 4      * param b: The second integer
 5      * return: The sum of a and b
 6      */
 7     public int aplusb(int a, int b) {
 8         if (a == 0) return b;
 9         int sum = a ^ b;
10         int carry = (a & b) << 1;
11         return aplusb(carry, sum);
12     }
13 };

 

posted @ 2016-07-06 02:21  北叶青藤  阅读(192)  评论(0)    收藏  举报