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

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

    int Add(int a, int b)
    {
        while (b)
        {
            int carry = a & b;
            a = a ^ b;
            b = carry << 1;
        }
        return a;
    }
    

提交回复
热议问题