Is this how the + operator is implemented in C?

后端 未结 9 1721
抹茶落季
抹茶落季 2021-01-30 06:21

When understanding how primitive operators such as +, -, * and / are implemented in C, I found the following snippet from an

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-01-30 06:48

    When you add two bits, following is the result: (truth table)

    a | b | sum (a^b) | carry bit (a&b) (goes to next)
    --+---+-----------+--------------------------------
    0 | 0 |    0      | 0
    0 | 1 |    1      | 0
    1 | 0 |    1      | 0
    1 | 1 |    0      | 1
    

    So if you do bitwise xor, you can get the sum without carry. And if you do bitwise and you can get the carry bits.

    Extending this observation for multibit numbers a and b

    a+b = sum_without_carry(a, b) + carry_bits(a, b) shifted by 1 bit left
        = a^b + ((a&b) << 1)
    

    Once b is 0:

    a+0 = a
    

    So algorithm boils down to:

    Add(a, b)
      if b == 0
        return a;
      else
        carry_bits = a & b;
        sum_bits = a ^ b;
        return Add(sum_bits, carry_bits << 1);
    

    If you get rid of recursion and convert it to a loop

    Add(a, b)
      while(b != 0) {
        carry_bits = a & b;
        sum_bits = a ^ b;
    
        a = sum_bits;
        b = carrry_bits << 1;  // In next loop, add carry bits to a
      }
      return a;
    

    With above algorithm in mind explanation from code should be simpler:

    int t = (x & y) << 1;
    

    Carry bits. Carry bit is 1 if 1 bit to the right in both operands is 1.

    y ^= x;  // x is used now
    

    Addition without carry (Carry bits ignored)

    x = t;
    

    Reuse x to set it to carry

    while(x)
    

    Repeat while there are more carry bits


    A recursive implementation (easier to understand) would be:

    int add(int x, int y) {
        return (y == 0) ? x : add(x ^ y, (x&y) << 1);
    }
    

    Seems that this function demonstrates how + actually works in the background

    No. Usually (almost always) integer addition translates to machine instruction add. This just demonstrate an alternate implementation using bitwise xor and and.

提交回复
热议问题