Example #1
0
int main()
{
    int a, b;
    scanf("%d %d", &a, &b);
    printf("%d\n", aplusb(a, b));

    return 0;
}
Example #2
0
int aplusb(int a, int b)
{
    if (b == 0)
        return a;

    int sum = a ^ b;             //a 和 b 二进制各位相加, 不考虑进位, 使用异或运算
    int carry = (a & b) << 1;    //a 和 b 二进制各位相加的进位情况
    aplusb(sum, carry);          //递归相加直到进位为0
}
Example #3
0
int main()
{
    int a = 4;
    int b = 20;
    
    printf("please input a & b: ");
    scanf("%d %d", &a , &b);
    printf("a=%d, b=%d, a+b=%d\n", a, b, aplusb(a, b));

    return 0;
}
Example #4
0
 /*
  * @param a: The first integer
  * @param b: The second integer
  * @return: The sum of a and b
  */
 int aplusb(int a, int b) {
     // write your code here, try to do it without arithmetic operators.
     if (b == 0)
     {
            return a;
     }
     else {
         int c = a ^ b;
         int carry = (a & b) << 1;
         return aplusb(c,carry);
     }
 }
Example #5
0
 /*
  * @param a: The first integer
  * @param b: The second integer
  * @return: The sum of a and b
  */
 int aplusb(int a, int b) {
     if (b == 0) {
         return a;
     }
     return aplusb(a ^ b, (a & b) << 1);
 }