Add without addition, subtraction, multiplication and division
Topic description
Write a function to calculate the sum of two integers. It is required not to use the +, -, *, / four arithmetic symbols in the function body.
topic Link : not do addition and subtraction, multiplication and division
code
/**
* 标题:不用加减乘除做加法
* 题目描述
* 写一个函数,求两个整数之和,要求在函数体内不得使用+、-、*、/四则运算符号。
* 题目链接:
* https://www.nowcoder.com/practice/59ac416b4b944300b617d4f7f111b215?tpId=13&&tqId=11201&rp=1&ru=/ta/coding-interviews&qru=/ta/coding-interviews/question-ranking
*/
public class Jz48 {
/**
* a ^ b 表示没有考虑进位的情况下两数的和,(a & b) << 1 就是进位。
* 递归会终止的原因是 (a & b) << 1 最右边会多一个 0,那么继续递归,进位最右边的 0 会慢慢增多,最后进位会变为 0,递归终止。
*/
public int add(int num1, int num2) {
return num2 == 0 ? num1 : add(num1 ^ num2, (num1 & num2) << 1);
}
public static void main(String[] args) {
Jz48 jz48 = new Jz48();
System.out.println(jz48.add(2, 6));
}
}
[Daily Message] We must believe that the seemingly undisturbed day after day will surely show us the meaning of persistence one day.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。