> For the complete documentation index, see [llms.txt](https://adit0503.gitbook.io/leetcode/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://adit0503.gitbook.io/leetcode/371.-sum-of-two-integers.md).

# 371. Sum of Two Integers

Easy

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

## :innocent: [Solution](https://leetcode.com/problems/sum-of-two-integers/)

{% tabs %}
{% tab title="O(ab)" %}

```python
class Solution:
    def getSum(self, a: int, b: int) -> int:

        MAX = 0x7FFFFFFF
        MIN = 0x80000000
        mask = 0xFFFFFFFF
        
        while b != 0:
            a, b = (a ^ b) & mask, ((a & b) << 1) & mask

        return a if a <= MAX else ~(a ^ mask)
```

{% endtab %}
{% endtabs %}
