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

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

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

    #include
    
    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

提交回复
热议问题