Skip to content
BASA DSA

Sum of Two Integers

MediumBit Manipulation

Return a+b without using addition or subtraction operators in the implementation. Use 32-bit two-complement bit operations.

Example 1

Input
a = 5
b = -3
Output
2

Explanation

Combining positive 5 and negative -3 gives 2.

Example 2

Input
a = -7
b = -8
Output
-15

Explanation

Two negative operands give -15; signed conversion must preserve its sign.

Example 3

Input
a = 0
b = 0
Output
0

Explanation

Zero operands create neither a sum bit nor a carry.

Example 4

Input
a = 999
b = 1
Output
1000

Explanation

Carry propagation through the low bits turns 999 and 1 into 1000.

Constraints

  • -1000 <= a,b <= 1000

Function contractgetSumArguments: json, json
Loading draft…
SolutionRuns on your device
Loading draft…