Sum of Two Integers Without +

MediumBit Manipulationbit arithmetic

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 needed

Problem

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

Input: a = 1, b = 2
Output: 3
`1 ^ 2 = 3` and `(1 & 2) << 1 = 0`, so there is no carry and the answer is `3`.
Input: a = -2, b = 3
Output: 1
Treating both as 32-bit values, the XOR and carry loop resolves to `1`.
Input: a = 0, b = 0
Output: 0
Adding two zeros produces zero with no carry.

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.