In this article, we will take a look on another interview question about adding two numbers, but without using + or ++ operator. Interview starts with a simple statement, Can you write a function to add two numbers (integers) without using + or plus arithmetic operator in Java? If you are good in maths, it wouldn’t take more than a second to say that, we can use subtraction or - operator to add two numbers because a-(-b)== a+b. Well that’s correct, but real question starts when interviewer quickly points out that, you can not use any arithmetic operator including +,-,*,/++ or --. Programming and Coding questions are integral part of any Java interview. You should always expect couple of questions like this, e.g. swapping two numbers without using temp variable. If you have been giving interviews, then you know that, it will eventually comes downs to bitwise operator in Java. Yes, we can add two numbers by using bitwise and bitshift operators, which is not arithmetic. Interviewer, will be happy by hearing bitwise operator, but he would like to see the code. Well, if you know binary arithmetic or how to add numbers in binary format, you may be familiar with fact than sum of two numbers can be obtained by using XOR operation and carry, by using AND operation. This is the fact, you must remember to solve this question or add two integers without using any arithmetic operator e.g. plus, minus etc. Sometime interviewer, may ask you to write both iterative and recursive solution of same question, Since recursion is another confusing programming technique, it's favored more during interviews. In this Java tutorial, we will see both recursive and iterative version of our add method, which calculate sum of two numbers without using arithmetic operator, but using bitshift and bitwise operators in Java.
Iterative solution uses any kind of loop, e.g. for, while or do-while. As I said, sum of two integer can be obtained by XOR bitwise operator and carry can be obtained but AND bitwise operator. We also need to use signed left shift Java operator to add carry into sum. Here is code for iterative method to add two integers without using plus or minus operator :
Code is self explanatory, we are calculating carry and keeping it in a separate variable, than we are storing sum of two numbers into variable a, and shifts carry to 1 bit by using signed left shift operator, In order to add into sum.
Following method uses recursion programming technique to calculate sum of two numbers without arithmetic operator e.g. +, - etc. It's recursive version of our earlier iterative solution. Just like in loop, we are testing for b=0, here also we are doing same thing. We have just combined & bitwise and << signed left shift operator calculating and shifting carry.
How to add two integers without arithmetic operator Java Example
Here is complete code example, which includes both iterative and recursive method along with a simple JUnit test. I have tested both add methods for couple of edge cases e.g. adding negative numbers, Integer.MAX_VALUE, adding zero etc.
And here is JUnit test case to test add() and addIterative() method, If you notice carefully, I have used static import feature of Java 1.5, to import various assert methods like assertEquals(expected, actual). By the way, I just realized that I made an error here, which won't affect the result but it's a mistake. If you can point it out then let me know :
That's all on How to add two numbers without using plus + arithmetic operator in Java. As I said, you need to remember that sum of two integers in binary is equal to XOR of two numbers and carry is equal to AND operation of two numbers. By using bitwise and bitshift operator in Java, you can easily calculate sum of two numbers without any arithmetic operator.
Other Java Coding Interview Questions from JavarevisitedCheck if a number is even or odd without using remainder operator in Java