Calculate the sum of two integers *a* and *b*, but you are **not allowed** to use the operator `+`

and `-`

.

**Example 1:**

Input:a = 1, b = 2Output:3

**Example 2:**

Input:a = -2, b = 3Output:1

The description was taken from __https://leetcode.com/problems/sum-of-two-integers/.__

`#O(1) Time, O(1) Space`

**class** **Solution**:

` `**def** **getSum**(self, a: int, b: int) -> int:

` x, y = abs(a), abs(b)`

` `**if** x < y:

` `**return** self.getSum(b, a)

` `**if** a > **0**:

` sign = `**1**

` `**else**:

` sign = -`**1**

` `

` `**if** a * b >= **0**:

` `**while** y **is** **not** **0**:

` x, y = x ^ y, (x & y) << `**1**

` `**else**:

` `**while** y **is** **not** **0**:

` x, y = x ^ y, (~x & y) << `**1**

` `**return** x * sign

If we are given a problem and are asked to perform a specific operation without using its operator, that is usually an indication that bit manipulation will be required.

Inherently, there are a lot of use cases when summing both positive and negative numbers because

and **a**

could be lesser or greater than the other, one could be negative, both could be negative, both could be positive, so we want to reduce the number of use cases to two. **b**

We can simplify this to strictly dealing with the sum or the difference of two positive numbers. It will either be

plus **x **

where **y**

is greater or **x **

minus **x**

where **y**** x** is greater.

We’ll start by converting the two integers

and **a**

to their absolute values and copying them to **b**

and **x**

.**y**

` x, y = abs(a), abs(b)`

Both of the two use cases that we're focusing on require

to be the greater, so we’ll check if that is the case and if not, we’ll re-run our function with the inputs switched.**x**

` `**if** x < y:

` `**return** self.getSum(b, a)

Next, let's check our initial

value and see if it’s negative or positive so that we’ll know what sign our result integer should have.*a*

` `**if** a > **0**:

` sign = `**1**

` `**else**:

` sign = -`**1**

Next, we’ll see if the product of our two input integers is positive.

If it is positive then we know we’re adding two positive integers. If it is negative then we know we’re taking the difference of two positive integers instead.

` `

**if** a * b >= **0**:

For both our computations, we’ll use a loop that will run while there are still values to carry.

` `

**while** y **is** **not** **0**:

The addition computation will consist of setting x equal to the result of

, so that we can isolate the bits that aren't both one within each number and won't need to be carried when summed.**x XOR y**

After isolating the bits that won't need to be carried when summed into

, let's now isolate the ones that do into **x**

. We'll shift them to the left and carry them so that we can continue summing them with the more significant bits within the next iteration.**y**

This is similar to how we would add two numbers on paper.

` x, y = x ^ y, (x & y) << `

**1**

For the difference computation, where the product* *is a negative result, the only difference from the addition computation is we’ll be using the negation of

due to our second use case. **x**

` x, y = x ^ y, (~x & y) << `**1**

Once we have our final result, we will want to multiply it by our sign before returning so that we return a negative or positive value based on what

was originally.**a **

` `

**return** x * sign

The solution has a constant time and space complexity because we are only dealing with two 32-bit integers so we will at most have thirty-two iterations.