Sum of Two Integers
Given two integers a
and b
, return the sum of the two integers without using the operators +
and -
.
Constraints: -1000 <= a, b <= 1000
Shell
The Gist of the Problem
The “gist” of this problem is really one involving number theory. If you aren’t familiar with number theory then we’re probably in some hot water, we’ll need at least some familiarity with basic numbers and their bases to progress. We have two integers, represented in base two, and we must add them together. Now is when intuition (remember, the creed of my whole personal brand?) comes in handy.
You’ve probably practice the algorithm necessary to accomplish this task already with base ten numbers in grade school. Remember?
10
30 +
———-
40
101
123 +
———
214
Line up integers on top of one another, add the integers in each column, if the integer you’ve added flows over the base of your number system (in the examples above we’re in base 10), “carry” it over into the next column and add it there, continuing right to left until we’ve progressed over the entire number. If you end up with one number that has a lesser degree (fewer integers that comprise it) that the other, you can simply pad with 0s.
Now maybe formally describing why the above works would be a bit difficult, but intuitively, if you consider what we’re doing, it makes sense. Right?
A base ten number is a representation of the abstract concept of “ten”. Conceptually it involves a model that counts discrete natural entities. When we have ten of those we draw a delineation in our model. Why’d we chose ten? Well you have ten fingers. So maybe that was just our first inclination based on when we started counting things so long ago. Then we started writing them down. And we kept using ten because it was natural. Then we discovered computers, and we started using two.
But I digress. Your model is such that you have ten of something and that forms a boundary for the numerical model you use to conceptualize the world. If you want to combine two entities in ten based models and you find that on one end of your model you go above your arbitrary delimiter (ten), you’ve just discovered you’ve surpassed the first boundary to your model and you must “overflow” it into the next column.
That description made almost no sense right. Because it’s intuitive. And it’s very difficult to describe intuition with natural language, even though I can see it in my head (remember, the creed of my whole personal brand?).
So… we have an algorithm and intuition tells us it should work with base 2 as well (why not? It works with one number system, why not another?). Are we correct? Well it’s easy to tell.
0101 - 5
1001 - 9
———
1110 - 14
0010 0101 - 37
0010 1100 - 44
————-
0101 0001 - 81
Well here’s a few test cases… looks good so far. Above we have ever “imaginable” case tested (a 1 above a 1, a 1 above a 0 and vice versa, and a 0 above a 0), so it looks like we’re ok.
Now to implement it.
class Solution { public: int getSum(int a, int b) { } };