Sum of Two Integers
Given two integers a and b, return the sum of the two integers without using the operators + and -. Use bitwise operations instead.

30:00

Sum of Two Integers
medium
Topics
Companies

Given two integers a and b, return the sum of the two integers without using the operators + and -. Use bitwise operations instead.

Example 1:
Input: {"a":1,"b":2}
Output: 3
Constraints:
  • 1000a,b1000-1000 \leq a, b \leq 1000

Input
arr ={"a":1,"b":2}

Start: a = 1, b = 2

a:

0
0
0
0
0
0
0
1

= 1

b:

0
0
0
0
0
0
1
0

= 2

a XOR b:

0
0
0
0
0
0
1
1

= 3

carry:

0
0
0
0
0
0
0
0

= 0

Step 0: XOR gives sum without carry, AND + shift gives carry

Current result: 3

Algorithm: a XOR b = sum without carry, (a AND b) << 1 = carry
Variables
No variables to display
DepthFunction Call
Stack empty
0/2