这道题的关键有几点:
-
通过异或操作获取在不进位的情况下,各位的值;
-
通过相与加移位来来获取各个进位项;
-
重复上面的操作,直到进位项为 0 为止。
思考题:思考如何通过位运算来实现加减乘除?
Calculate the sum of two integers a and b, but you are not allowed to use the operator +
and -
.
Example 1:
Input: a = 1, b = 2 Output: 3
Example 2:
Input: a = -2, b = 3 Output: 1
link:{sourcedir}/_0371_SumOfTwoIntegers.java[role=include]