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

后端 未结 21 1870
南笙
南笙 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:15

    With given answers above, it can be done in single line code:

    int add(int a, int b) {
        return (b == 0) ? a : add(a ^ b, (a & b) << 1);
    }
    
    0 讨论(0)
  • 2020-11-27 11:18

    Code to implement add,multiplication without using +,* operator; for subtraction pass 1's complement +1 of number to add function

    #include<stdio.h>
    
    unsigned int add(unsigned int x,unsigned int y)
    {
             int carry=0;
        while (y != 0)
        {
    
            carry = x & y;  
            x = x ^ y; 
            y = carry << 1;
        }
        return x;
    }
    int multiply(int a,int b)
    {
        int res=0;
        int i=0;
        int large= a>b ? a :b ;
        int small= a<b ? a :b ;
        for(i=0;i<small;i++)
        {
               res = add(large,res);                    
        }
        return res;
    }
    int main()
    {
        printf("Sum :: %u,Multiply is :: %d",add(7,15),multiply(111,111));
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 11:19
    #include<stdio.h>
    
    int add(int x, int y) {
        int a, b;
        do {
            a = x & y;
            b = x ^ y;
            x = a << 1;
            y = b;
        } while (a);
        return b;
    }
    
    
    int main( void ){
        printf( "2 + 3 = %d", add(2,3));
        return 0;
    }
    
    0 讨论(0)
  • 2020-11-27 11:21
    ## to add or subtract without using '+' and '-' ## 
    #include<stdio.h>
    #include<conio.h>
    #include<process.h>
    
    void main()
    {
        int sub,a,b,carry,temp,c,d;
    
        clrscr();
    
        printf("enter a and b:");
        scanf("%d%d",&a,&b);
    
        c=a;
        d=b;
        while(b)
        {
            carry=a&b;
            a=a^b;
            b=carry<<1;
        }
        printf("add(%d,%d):%d\n",c,d,a);
    
        temp=~d+1;  //take 2's complement of b and add it with a
        sub=c+temp;
        printf("diff(%d,%d):%d\n",c,d,temp);
        getch();
    }
    
    0 讨论(0)
  • 2020-11-27 11:22

    You can use double negetive to add two integers for example:

    int sum2(int a, int b){
        return -(-a-b);
    }
    
    0 讨论(0)
  • 2020-11-27 11:23

    This is something I have written a while ago for fun. It uses a two's complement representation and implements addition using repeated shifts with a carry bit, implementing other operators mostly in terms of addition.

    #include <stdlib.h> /* atoi() */
    #include <stdio.h>  /* (f)printf */
    #include <assert.h> /* assert() */
    
    int add(int x, int y) {
        int carry = 0;
        int result = 0;
        int i;
    
        for(i = 0; i < 32; ++i) {
            int a = (x >> i) & 1;
            int b = (y >> i) & 1;
            result |= ((a ^ b) ^ carry) << i;
            carry = (a & b) | (b & carry) | (carry & a);
        }
    
        return result;
    }
    
    int negate(int x) {
        return add(~x, 1);
    }
    
    int subtract(int x, int y) {
        return add(x, negate(y));
    }
    
    int is_even(int n) {
        return !(n & 1);
    }
    
    int divide_by_two(int n) {
        return n >> 1;
    }
    
    int multiply_by_two(int n) {
        return n << 1;
    }
    
    int multiply(int x, int y) {
        int result = 0;
    
        if(x < 0 && y < 0) {
            return multiply(negate(x), negate(y));
        }
    
        if(x >= 0 && y < 0) {
            return multiply(y, x);
        }
    
        while(y > 0) {
            if(is_even(y)) {
                x = multiply_by_two(x);
                y = divide_by_two(y);
            } else {
                result = add(result, x);
                y = add(y, -1);
            }
        }
    
        return result;
    }
    
    int main(int argc, char **argv) {
        int from = -100, to = 100;
        int i, j;
    
        for(i = from; i <= to; ++i) {
            assert(0 - i == negate(i));
            assert(((i % 2) == 0) == is_even(i));
            assert(i * 2 == multiply_by_two(i));
            if(is_even(i)) {
                assert(i / 2 == divide_by_two(i));
            }
        }
    
        for(i = from; i <= to; ++i) {
            for(j = from; j <= to; ++j) {
                assert(i + j == add(i, j));
                assert(i - j == subtract(i, j));
                assert(i * j == multiply(i, j));
            }
        }
    
        return 0;
    }
    
    0 讨论(0)
提交回复
热议问题