Sum of Two Integers

Given two integers a and b, return their sum without using the operators + and -. Pattern focus: Bit Addition. Use XOR for partial sums and AND-shift for carries until the carry becomes zero.

Input Format

a, b = integers

Output Format

sum of a and b without using + or -

Constraints

  • 1 <= input size <= 10^5
  • -10^9 <= numeric values <= 10^9
  • a, b must satisfy the format described in inputFormat.

Examples

Example 1:

Input:

a = 1
b = 2

Output:

3

Explanation:

The bitwise sum of 1 and 2 is 3.

Example 2:

Input:

a = -2
b = 3

Output:

1

Explanation:

The algorithm correctly handles negative values.

Loading...
Sum of Two Integers - Bit Manipulation