分析:
对于x和y
1.首先计算各位相加但不计进位;
2.记下进位;
3.把前步的结果相加。
1 int add(int num1, int num2) 2 { 3 int sum, carry; 4 do 5 { 6 sum = num1^num2; 7 carry = (num1&num2)<<1; 8 num1 = sum; 9 num2 = carry; 10 }while (carry!=0) 11 return num1; 12 }
分析:
对于x和y
1.首先计算各位相加但不计进位;
2.记下进位;
3.把前步的结果相加。
1 int add(int num1, int num2) 2 { 3 int sum, carry; 4 do 5 { 6 sum = num1^num2; 7 carry = (num1&num2)<<1; 8 num1 = sum; 9 num2 = carry; 10 }while (carry!=0) 11 return num1; 12 }