Q8. How to add two numbers without using plus operator?

We can add two numbers without using plus operator, many of us quickly reply with below answer.

a – (-b)

But what we will do, if interviewer slightly twist question like this:

How to add two numbers without using arithmetic operator?

add(int a, int b)

{

    while (b != 0)

    {

        int carry = a & b;

        a = a ^ b;

        b = carry << 1;

    }

    return a;

}

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.