Sum of Two Integers
Return the sum of two integers a and b without using the operators + or -.
Open official problem prompt ↗Reproduce integer addition using only bitwise operations, correctly handling negative numbers.
It is grade-school column addition in base 2: XOR is 'write the digit', AND-then-shift-left is 'carry the one to the next column'. You keep sweeping columns until nothing is left to carry.
- Input
- a = 1, b = 2
- Output
- 3
- Why
- 1 + 2 = 3; XOR gives the sum bits (01 ^ 10 = 11 = 3) and there is no carry, so the result is 3.
-1000 <= a, b <= 1000