Skip to main content

How to add 2 numbers without using an arithmetic operator?

 Guess you have to add two integers or numbers but without using the arithmetic operator. You cannot use any arithmetic operator like +, -, ++, --, ... etc. How will you do that? This question was asked in Microsoft Online Assessment.

For solving this problem we have to go back to our Digital Electronics concepts where for adding two bits, we can do the XOR(^) of those bits. if they have a carry, we can find that out by applying AND(&) of them. So, this is the simple concept of Half Adder which is used to add 2 single bits. 

Input: 3 5

Output: 8

C++ code for this problem:



#include <bits/stdc++.h>
using namespace std;

int main()
{
    int x, y, carry;
    cin >> x >> y;
    while (y != 0)
    {
        carry = x & y;
        x = x ^ y;
        y = carry << 1;
    }
    cout << x << "\n";
    return 0;
}

Explanation:

Input: 3 5

For x=3 and y=5:

Loop1: y = 5 != 0,
            carry = 3 & 5 = 1,
            x = 3 ^ 5 = 6,
            y = 2.

Loop2: y = 2 != 0,
            carry = 6 & 2 = 2,
            x = 6 ^ 2 = 4,
            y = 4.

Loop3: y= 4!=0,
            carry = 4 & 4 = 4,
            x = 4 ^ 4 = 0,
            y = 8.

Loop4: y = 8 != 0,
            carry = 8 & 0 = 0,
            x = 0 ^ 8 = 8,
            y = 0.

As y==0 then the loop will end and the answer will be x which equals 8.

Output: 9

If you have any questions, ask them in the comments.

Comments

Popular posts from this blog

3 hacks to wake up early in the morning

So this is what everyone says that we have to wake up early in the morning. Yes, it's good to wake up early and this could be the best decision you will take in your life.   I have read this somewhere that " If you want to change your life, do nothing just start waking up early in the morning and things will start happening ". So yes waking up early in the morning is going to help you a lot and it will surely help in changing your life. Now the question that arises is how we can wake up early in the morning? We all want to wake up early at 5 or 6 Am in the morning but when the clock hits 5 and your alarm makes the sound, your mind says just 5 minutes more and these 5 minutes end up at 1 hour.  So the question is how can we make it to wake up early in the morning. So in this article, I'm sharing 3 hacks that can surely help you in achieving your goal of waking up early in the morning.  The secret of waking up early doesn't lie in the process of waking up early in ...

Flow Network | Ford-Fulkerson Algorithm

Flow Network is a directed graph in which each edge has a capacity and each edge receives a flow.  In this blog, we will discuss the following things: Introduction of:  network, max-flow problem capacity, flow Ford-Fulkerson method pseudo code, residual networks, augmenting paths cuts of networks Introduction -  Network We can understand Network by looking into some examples: liquids flowing in pipes, parts through assembly lines in a factory, the current through electrical network. Flow Network: Directed graph, G=(V, E)                              where V is set of vertices,                                            E is set of Edges. Max-Flow problem Max-flow problem is about finding the feasible flow through a flow network and with maximum possible flow ra...

Reddit raises $250 million

 Reddit has raised $250 Million in a new funding round. This is the series E round of financing for Reddit. Reddit Reddit has become more popular and gained more public attention after the GameStop stock incident. The subreddit r/WallStreetBets played a big role in the rise of the value of GameStop stock.  Reddit has completed 16 years and it has raised around $800 million to date. Reddit has raised $300 million in February 2019 in series D round from Tencent. Reddit has said that this financing is from existing and new investors.  Reddit has posted announcement through a blog post which you can read here . They have also given insights about the future plan as they have said, We decided that now was the right opportunity to make strategic investments in Reddit including video, advertising, consumer  products, and expanding into international markets. Reddit Super Bowl Ad Reddit also ran a 5-second Super Bowl ad consisting of a single static image that looked like a ...