How to add two numbers without using ++ or + or another arithmetic operator

后端 未结 21 1837
南笙
南笙 2020-11-27 10:48

How do I add two numbers without using ++ or + or any other arithmetic operator?

It was a question asked a long time ago in some campus interview. Anyway, today some

相关标签:
21条回答
  • 2020-11-27 11:32

    If you're feeling comedic, there's always this spectacularly awful approach for adding two (relatively small) unsigned integers. No arithmetic operators anywhere in your code.

    In C#:

    static uint JokeAdder(uint a, uint b)
    {
        string result = string.Format(string.Format("{{0,{0}}}{{1,{1}}}", a, b), null, null);
        return result.Length;
    }
    

    In C, using stdio (replace snprintf with _snprintf on Microsoft compilers):

    #include <stdio.h>
    unsigned int JokeAdder(unsigned int a, unsigned int b)
    {
        return snprintf(NULL, 0, "%*.*s%*.*s", a, a, "", b, b, "");
    }
    
    0 讨论(0)
  • 2020-11-27 11:34

    For unsigned numbers, use the same addition algorithm as you learned in first class, but for base 2 instead of base 10. Example for 3+2 (base 10), i.e 11+10 in base 2:

       1         ‹--- carry bit
       0 1 1     ‹--- first operand (3)
     + 0 1 0     ‹--- second operand (2)
     -------
       1 0 1     ‹--- total sum (calculated in three steps)
    
    0 讨论(0)
  • 2020-11-27 11:35
    int add_without_arithmatic(int a, int b)
    {
        int sum;
        char *p;
        p = (char *)a;
        sum = (int)&p[b];
        printf("\nSum : %d",sum);
    }
    
    0 讨论(0)
  • 2020-11-27 11:37
    short int ripple_adder(short int a, short int b)
    {
        short int i, c, s, ai, bi;
    
        c = s = 0;
    
        for (i=0; i<16; i++)
        {
            ai = a & 1;
            bi = b & 1;
    
            s |= (((ai ^ bi)^c) << i);
            c = (ai & bi) | (c & (ai ^ bi));
    
            a >>= 1;
            b >>= 1;
        }
        s |= (c << i);
        return s;
    }
    
    0 讨论(0)
  • 2020-11-27 11:39

    Well, to implement an equivalent with boolean operators is quite simple: you do a bit-by-bit sum (which is an XOR), with carry (which is an AND). Like this:

    int sum(int value1, int value2)
    {
        int result = 0;
        int carry = 0;
        for (int mask = 1; mask != 0; mask <<= 1)
        {
            int bit1 = value1 & mask;
            int bit2 = value2 & mask;
            result |= mask & (carry ^ bit1 ^ bit2);
            carry = ((bit1 & bit2) | (bit1 & carry) | (bit2 & carry)) << 1;
        }
        return result;
    }
    
    0 讨论(0)
  • 2020-11-27 11:40

    This can be done recursively:

    int add_without_arithm_recursively(int a, int b)
    {
        if (b == 0) 
            return a;
    
        int sum = a ^ b; // add without carrying
        int carry = (a & b) << 1; // carry, but don’t add
        return add_without_arithm_recursively(sum, carry); // recurse
    }
    

    or iteratively:

    int add_without_arithm_iteratively(int a, int b)
    {
        int sum, carry;
    
        do 
        {
            sum = a ^ b; // add without carrying
            carry = (a & b) << 1; // carry, but don’t add
    
            a = sum;
            b = carry;
        } while (b != 0);
    
        return a;
    }
    
    0 讨论(0)
提交回复
热议问题