这道题目很有意思,思路和方法都堪称完美:
/**
* Created by clearbug on 2018/2/26.
*/
public class Solution {
public static void main(String[] args) {
Solution s = new Solution();
System.out.println(s.add(5, 17));
System.out.println(s.add(1, 0));
System.out.println(s.add(1, 9));
System.out.println(s.add(1, 8));
System.out.println(s.add(1, -1));
System.out.println(s.add(1, -2));
}
public int add(int num1, int num2) {
int sum, carry;
do {
sum = num1 ^ num2;
carry = (num1 & num2) << 1;
num1 = sum;
num2 = carry;
} while (carry != 0);
return num1;
}
}