Sum of Two Integers Without +
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
Given two signed integers a and b, return their sum a + b **without** using the + or - operators anywhere in your solution.
You must rely on bitwise operations only. The core idea is that adding two numbers can be broken into two pieces:
- The **sum without carrying**, which is exactly the XOR of the two values (a ^ b).
- The **carry**, which is the AND of the two values shifted left by one ((a & b) << 1).
Repeatedly combine these two parts until there is no carry left, and the result is the final sum. Negative numbers are handled naturally when you treat the integers as fixed-width 32-bit two's-complement values.
Implement the function so that it works for any combination of positive, negative, and zero inputs within the 32-bit signed range.
Examples
Constraints
- -1000 <= a, b <= 1000 (values stay within the 32-bit signed range)
- You may not use the `+` or `-` operators
- The result is guaranteed to fit in a 32-bit signed integer
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.