1 /************************************************************************* 2 > File Name: 47_AddTwoNumbers.c 3 > Author: Juntaran 4 > Mail: [email protected] 5 > Created Time: 2016年09月04日 星期日 21时33分07秒 6 ************************************************************************/ 7 8 #include <stdio.h> 9 10 // 移位求两数相加 11 int ADD(int num1, int num2) 12 { 13 int sum, carry; 14 if (num1 == 0) 15 return num2; 16 if (num2 == 0) 17 return num1; 18 19 while (num2 != 0) 20 { 21 sum = num1 ^ num2; 22 carry = (num1 & num2) << 1; 23 24 num1 = sum; 25 num2 = carry; 26 } 27 return sum; 28 } 29 30 int main() 31 { 32 int num1 = 5; 33 int num2 = 37; 34 35 int sum = ADD(num1, num2); 36 printf("sum is %d\n", sum); 37 return 0; 38 }
时间: 2024-10-11 10:20:01