【题目描述】
Write a function that add two numbers A and B. You should not use + or any arithmetic operators.
There 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?
Can I use bit operation?
【题目大意】
给出两个整数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(String[] args) {
System.out.println(plus(3,0));
}
}