您的位置:首页 > 编程语言

【每天一道编程系列-2018.3.11】—— A + B Problem

2018-03-11 23:36 447 查看
【题目描述】

Write a function that add two numbers A and B. You should not use 
+
 or any arithmetic operators.  NoticeThere is no need to read data from standard input stream. Both parameters are given in function 
aplusb
, you job is to calculate the sum and return it.Clarification
Are a and b both 
32-bit
 integers?Yes.
Can I use bit operation?Sure you can.

【题目大意】

给出两个整数a和b, 求他们的和, 但不能使用 
+
 等数学运算符。
 注意事项你不需要从输入流读入数据,只需要根据
aplusb
的两个参数a和b,计算他们的和并返回就行。说明a和b都是 
32位
 整数么?是的
我可以使用位运算符么?当然可以

【本题答案】
package blog;

/**
* @author yesr
* @create 2018-03-11 下午11:28
* @desc
**/
public class Test0310 {

/*
* @param a: The first integer
* @param b: The second integer
* @return: The sum of a and b
*/
private static int plus(int a, int b) {
if (b == 0)
return a;
else
return plus(a^b, (a&b) << 1);
}

public static void main(S
a0e7
tring
[] args) {
System.out.println(plus(3,0));
}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  比特位操作