剑指offer之求两个数之和(不能使用四则运算)
1 题目
剑指offer之求两个数之和(不能使用四则运算)
2 代码实现
#include<stdio.h>
int add(int num1, int num2)
{
int sum1;
int carry;
do
{
sum1 = (num1 ^ num2);
carry = (num1 & num2) << 1;
num1 = sum1;
num2 = carry;
} while (carry != 0);
return num1;
}
int main()
{
int result = add(3, 5);
printf("result is %d\n", result);
return 0;
}
3 运行结果
result is 8
赞 (0)