Programmer: how to solve addition and subtraction using Bit manipulation

in coding-challenge •  7 years ago  (edited)

I was having problem understanding how to use bit manipulation to solve programming problems (in this case, they are addition and subtraction) until I had a converstation with my colleage. We had a really good and probably quite long converstation about how to come up with bit manipulation solution from scratch.

That's why I want to summarize everything here in order for me to understand them more clearly and hopefully help someones else who also are having similar problem.

Addition

So, let's start with Addition first.

Prob: How to implement `add(int a, int b)` function using bit manipulation?

Pretty clear, if my method receives 2 integers, it will return sum of those 2.

Now, how do you approach this problem? Hmm...


Let's try out some easy examples: (From now on, every nums I write is in binary)

Adding to binary numbers:

0 + 1 = 1 (easy)

0 + 0 = 0 (easy)

1 + 0 = 1 (easy)

1 + 1 = 0 (this is interesting because you have additonal 1 value after addition, how do we address that?)


Let's not worry about the additional `1` for now. Based on above calculations we can have a truth table of sum operator

x | y | x + y | carryover

0 | 0 |   0   |    0

0 | 1 |   1  |    0

1 | 0 |   1   |    0

1 | 1 |   0   |    1


How do you using bit manipulation to calculate the value of `+` operator? 

Looks similar? Yes it's `XOR` operator. So it's fair to say `x + y` equals `x^y + carryover`.

So the question is how do we calculate that carry over part? Let's take a closer look at it

`1 + 1` actually equals `10`. See the carry over `1` jumps to next slot which is multiplying with `2` (`2` is base system(binary), just like decimal is `10`: 5 + 7 = 1*10 + 2)

Ok, we are close, how to you represent "multiply with 2" using bit manipulation? Yes it's bit shifting (`<< 1` here we shift 1 bit to the left).


Let's look at above truth table again. If `x + y` is representation of `XOR` operator then what's carry over? It's `AND`.


Cool, finally we can complete our formula: `x + y` equals `x^y + (x&y << 1)`. And we keep doing it until there is no carry over (meaning `x&y << 1 == 0`) then at that time `x^y` is the result we are looking for.


Subtraction (To be continued)


Happy coding!


Hung Cao


Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

Congratulations @hungcao! You received a personal award!

1 Year on Steemit

Click here to view your Board of Honor

Do not miss the last post from @steemitboard:

Saint Nicholas challenge for good boys and girls

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @hungcao! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Vote for @Steemitboard as a witness to get one more award and increased upvotes!