Sum of Two Integers Without Arithmetic Operators
mediumSave
Bit ManipulationMath
Given two integers a and b, compute the sum of a and b without using the operators + and -. You can use any other standard operators such as bitwise operators.
Example 1
Input
[ 5, 3 ]
Output
8
Explanation. Summing 5 and 3 using bitwise operations results in 8.
Example 2
Input
[ -2, 3 ]
Output
1
Explanation. Summing -2 and 3 using bitwise operations results in 1.
Example 3
Input
[ 15, -5 ]
Output
10
Explanation. Summing 15 and -5 using bitwise operations results in 10.
Example 4
Input
[ 0, 0 ]
Output
0
Explanation. Summing 0 and 0 logically results in 0.
Follow-up: How would you modify your approach if subtraction was also required without using `-` operator?
Constraints:
The integers can be both positive, negative, or a mix of both.
- Views
- 4