首页 > 其他 > 详细

A + B Problem

时间:2016-07-06 09:50:28      阅读:232      评论:0      收藏:0      [点我收藏+]

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

 

A + B Problem

原文:http://www.cnblogs.com/beiyeqingteng/p/5645569.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!