What is the best way to add two numbers without using the + operator?

后端 未结 22 1516
情书的邮戳
情书的邮戳 2020-11-27 05:18

A friend and I are going back and forth with brain-teasers and I have no idea how to solve this one. My assumption is that it\'s possible with some bitwise operators, but n

相关标签:
22条回答
  • 2020-11-27 05:41

    Implemented in same way as we might do binary addition on paper.

    int add(int x, int y)
    {
        int t1_set, t2_set;
        int carry = 0;
        int result = 0;
        int mask = 0x1;
    
        while (mask != 0) {
            t1_set = x & mask;
            t2_set = y & mask;
            if (carry) {
               if (!t1_set && !t2_set) {
                   carry = 0;
                   result |= mask;
               } else if (t1_set && t2_set) {
                   result |= mask;
               }
            } else {
               if ((t1_set && !t2_set) || (!t1_set && t2_set)) {
                    result |= mask;
               } else if (t1_set && t2_set) {
                    carry = 1;
               }
            }
            mask <<= 1;
        }
        return (result);
    }
    

    Improved for speed would be below::

    int add_better (int x, int y)
    {
      int b1_set, b2_set;
      int mask = 0x1;
      int result = 0;
      int carry = 0;
    
      while (mask != 0) {
          b1_set = x & mask ? 1 : 0;
          b2_set = y & mask ? 1 : 0;
          if ( (b1_set ^ b2_set) ^ carry)
              result |= mask;
          carry = (b1_set &  b2_set) | (b1_set & carry) | (b2_set & carry);
          mask <<= 1;
      }
      return (result);
    }
    
    0 讨论(0)
  • 2020-11-27 05:45

    Was working on this problem myself in C# and couldn't get all test cases to pass. I then ran across this.

    Here is an implementation in C# 6:

    public int Sum(int a, int b) => b != 0 ? Sum(a ^ b, (a & b) << 1) : a;
    
    0 讨论(0)
  • 2020-11-27 05:46

    The reason ADD is implememted in assembler as a single instruction, rather than as some combination of bitwise operations, is that it is hard to do. You have to worry about the carries from a given low order bit to the next higher order bit. This is stuff that the machines do in hardware fast, but that even with C, you can't do in software fast.

    0 讨论(0)
  • 2020-11-27 05:47

    It is my implementation on Python. It works well, when we know the number of bytes(or bits).

    def summ(a, b):
        #for 4 bytes(or 4*8 bits)
        max_num = 0xFFFFFFFF
        while a != 0:
            a, b = ((a & b) << 1),  (a ^ b)
            if a > max_num:
                b = (b&max_num) 
                break
        return b
    
    0 讨论(0)
  • 2020-11-27 05:48

    Note, this would be for an adder known as a ripple-carry adder, which works, but does not perform optimally. Most binary adders built into hardware are a form of fast adder such as a carry-look-ahead adder.

    My ripple-carry adder works for both unsigned and 2's complement integers if you set carry_in to 0, and 1's complement integers if carry_in is set to 1. I also added flags to show underflow or overflow on the addition.

    #define BIT_LEN 32
    #define ADD_OK 0
    #define ADD_UNDERFLOW 1
    #define ADD_OVERFLOW 2
    
    int ripple_add(int a, int b, char carry_in, char* flags) {
        int result = 0;
        int current_bit_position = 0;
        char a_bit = 0, b_bit = 0, result_bit = 0;
    
        while ((a || b) && current_bit_position < BIT_LEN) {
            a_bit = a & 1;
            b_bit = b & 1;
            result_bit = (a_bit ^ b_bit ^ carry_in);
            result |= result_bit << current_bit_position++;
            carry_in = (a_bit & b_bit) | (a_bit & carry_in) | (b_bit & carry_in);
            a >>= 1;
            b >>= 1;
        }
    
        if (current_bit_position < BIT_LEN) {
            *flags = ADD_OK;
        }
        else if (a_bit & b_bit & ~result_bit) {
            *flags = ADD_UNDERFLOW;
        }
        else if (~a_bit & ~b_bit & result_bit) {
            *flags = ADD_OVERFLOW;
        }
        else {
            *flags = ADD_OK;
        }
    
        return result;
    }
    
    0 讨论(0)
  • 2020-11-27 05:49
    int add(int a, int b) {
       const char *c=0;
       return &(&c[a])[b];
    }
    
    0 讨论(0)
提交回复
热议问题