Java Interview Question: How to Add Two Numbers Without Using the Plus (+) Operator?
If a processor can add two numbers in a single instruction, what is it actually doing inside that instruction? An interviewer who asks you to add two integers without + wants you to answer that question, not just to find a clever trick.
Candidates often reach for loops, Math helpers or a++ in a while. Each of those either breaks the rules or fails on large and negative inputs. The reliable answer uses bitwise logic, and addition without plus operator in Java becomes easy once you see how a carry moves through binary digits.
This guide works through that logic, shows the code, traces a full example by hand, and covers the follow-up questions that tend to appear in Java coding interview questions on bitwise topics.
Table of Contents
- What the Interviewer Is Really Testing
- Adding Numbers the Way a Circuit Does
- The Three Operators You Need
- Java Solution Using a Loop
- Working Through 13 + 6
- Recursion, Negative Values and Overflow
- Follow-Up Questions That Usually Come Next
- Practice Path for Java Interviews
What the Interviewer Is Really Testing
The task is small, but it checks several skills together. You need to know how integers are stored in binary, you need to be comfortable with Java bitwise operators, and you need to design a loop that is guaranteed to stop.
It also shows whether you can explain your thinking aloud. A candidate who writes the right code but cannot say why it works usually scores lower than one who talks through the carry logic calmly.
Say one thing plainly during the interview: this is a learning exercise. In real code you would write a + b, because it compiles to one hardware instruction and anyone can read it.
Adding Numbers the Way a Circuit Does
Think about adding 1 and 1 on paper in binary. The answer is 10, which means a digit of 0 stays in the current column and a 1 is carried to the next. Every column works like this.
A hardware half adder splits the job into two independent results: the digit that stays, and the carry that moves. If you compute both for every column at once, you get a partial answer and a set of carries. Adding the carries (shifted one column left) to the partial answer may produce new carries, so you repeat until none are left.
That repeated "add the carry again" step is the entire algorithm.
The Three Operators You Need
|
S.No
|
Operator
|
Role in the algorithm
|
Example (binary)
|
|
1
|
^ (XOR)
|
Adds each column and ignores carries
|
1010 ^ 0110 = 1100
|
|
2
|
& (AND)
|
Finds columns where a carry is produced
|
1010 & 0110 = 0010
|
|
3
|
<< (left shift)
|
Moves the carry into the next column
|
0010 << 1 = 0100
|
XOR gives 1 when exactly one of the two bits is 1, which matches the "digit that stays" in every case except 1+1. AND gives 1 only when both bits are 1, which is exactly when a carry is generated. Shifting by one places that carry where it belongs.
Java Solution Using a Loop
Java
public class BitwiseSum {
static int sumWithBits(int x, int y) {
while (y != 0) {
int partial = x ^ y; // digits without carries
int carry = (x & y) << 1; // carries, moved one column left
x = partial;
y = carry;
}
return x;
}
public static void main(String[] args) {
System.out.println(sumWithBits(13, 6)); // 19
System.out.println(sumWithBits(-4, 9)); // 5
}
}
Here x always holds the running partial sum and y holds whatever carry still needs to be added in. When y becomes 0, nothing is left to carry, and x is the answer. Both new values are computed from the old x and y before either is overwritten, which is why the temporary variables are there.
Working Through 13 + 6
Interviewers frequently ask you to dry-run the code. Using 5-bit binary, 13 is 01101 and 6 is 00110.
pass x (partial sum) y (carry)
start 01101 00110
1 01011 01000
2 00011 10000
3 10011 00000 -> 19
In pass 1, XOR gives 01011 and the only column where both bits are 1 is the third from the right, so the carry becomes 01000. In pass 2, 01011 and 01000 collide in one column, which pushes a new carry to 10000. In pass 3, there is no collision left, so the carry is zero and the loop ends with 10011, which is 19.
Being able to produce a table like this on a whiteboard is the strongest answer you can give.
Recursion, Negative Values and Overflow
The same idea works recursively:
java
static int sumWithBits(int x, int y) {
return (y == 0) ? x : sumWithBits(x ^ y, (x & y) << 1);
}
Each call moves carries further left, so depth never exceeds the number of bits in the type.
Negative numbers need no extra code. Java stores int in 32-bit two's complement, and the XOR and AND operations simply act on those 32 bits. For an int, the loop finishes within 32 passes because carries eventually shift out past the leftmost bit.
Overflow behaves the same as it does with +. Adding 1 to Integer.MAX_VALUE wraps around to Integer.MIN_VALUE, and you can mention this to show you know the language's rules.
For long inputs, change the types and the logic stays identical.
Follow-Up Questions That Usually Come Next
Once you answer the main question, expect a few variations:
- Subtraction without -. Negating a number is ~b + 1, so a - b becomes sumWithBits(a, sumWithBits(~b, 1)).
- Multiplication without *. Use shift-and-add: for each set bit in one number, add the other number shifted by that bit's position.
- Is a - (-b) acceptable? Only if subtraction is allowed, so ask which operators are banned before choosing a workaround.
- Can you use Integer.sum or Math.addExact? Both rely on + internally, so most interviewers will not accept them here.
A common slip is overwriting x before the carry is computed, which silently produces wrong results. Another is forgetting the << 1, which leaves carries in the wrong column and gives incorrect sums or an endless loop.
Practice Path for Java Interviews
This problem is one of many Java logical programs built around bitwise operations. After it, try swapping two variables without a temporary one, counting the set bits in an integer, and testing whether a number is a power of two with (n & (n - 1)) == 0.
If you are following a Java course or a Java training program, spend real time on the bitwise operators before moving to frameworks. Interviews for Full Stack Java roles often begin with core Java logic like this before they reach Spring Boot or front-end work. Learners preparing for Java Full Stack Development positions benefit from practising such Java interview programs regularly, though no practice set can promise a particular result.
Frequently Asked Questions
1. Can I use Math.addExact or Integer.sum in this interview?
Usually not, because both use the + operator internally and interviewers expect a bitwise solution.
2. Does this method work for negative numbers?
Yes, because Java's int uses two's complement and the XOR and AND logic operates directly on those bits.
3. Is the bitwise method faster than the + operator?
No, since + compiles to a single hardware instruction, so this approach is only for understanding.
4. Can the same logic handle long values?
Yes, change the parameter and return types to long and the loop works unchanged.
5. What bitwise program should I practise next?
Counting set bits is a good next step, followed by swapping without a temporary variable and the power-of-two check.
Conclusion
Binary addition is XOR for the digits, AND plus a left shift for the carries, repeated until no carry remains. Remember that single idea and you can rebuild the code even if the exact lines slip your mind.
For your next step, dry-run sumWithBits(-4, 9) on paper in 8-bit binary and confirm your table ends at 5.
If the interviewer banned +, - and * all at once, would you build multiplication with shift-and-add or look for another approach, and why?
Follow NareshIT for more practical insights on technology, skills, and career development.