Description
Given two integers a and b, return the sum of the two integers without using the operators + and -. Use bit manipulation with XOR and AND operations.
Examples
Input:
a = 1, b = 2Output:
3Explanation:
1 + 2 = 3 using bit manipulation.
Input:
a = -5, b = 3Output:
-2Explanation:
Adding a negative number (-5) and a positive number (3) results in -2. The bit manipulation approach handles the sign conversion properly by using XOR for addition without carry and AND with left shift for carry propagation.
Input:
a = 0, b = 15Output:
15Explanation:
When one number is 0, the result is simply the other number. In bit manipulation terms, 0 XOR 15 = 15 with no carry bits, making this an edge case that demonstrates the base behavior of the algorithm.
Constraints
- •
-1000 ≤ a, b ≤ 1000