> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Sum of Two Integers Python Solution with Tests

> Tested Python solution for LeetCode 371 with 15 pytest cases. Generate a practice environment with lcpy.

LeetCode 371, Medium. Topics: Math, Bit Manipulation. [View on LeetCode](https://leetcode.com/problems/sum-of-two-integers/description/).

Generate this problem as a practice environment: tested reference solution, 15 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 371   # by problem number
lcpy gen -s sum_of_two_integers   # by problem name
```

## Problem

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

### Examples

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

```
Input: a = 2, b = 3
Output: 5
```

### Constraints

-1000 \<= a, b \<= 1000

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sum_of_two_integers/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/sum_of_two_integers/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(1) - constant time bit operations
    # Space: O(1) - no extra space used
    def get_sum(self, a: int, b: int) -> int:
        """
        Add two integers without using + or - operators.
        Uses bit manipulation approach:
        1. XOR gives us the sum without carry
        2. AND + left shift gives us the carry
        3. Repeat until no carry remains
        """
        # Handle 32-bit signed integer overflow
        mask = 0xFFFFFFFF

        while b != 0:
            # Calculate sum without carry
            sum_without_carry = (a ^ b) & mask
            # Calculate carry
            carry = ((a & b) << 1) & mask
            a = sum_without_carry
            b = carry

        # Handle negative result for 32-bit signed integer
        if a > 0x7FFFFFFF:
            a = ~(a ^ mask)

        return a
```

## Complexity

| Time                                | Space                      |
| ----------------------------------- | -------------------------- |
| O(1) - constant time bit operations | O(1) - no extra space used |

## Tags

[Blind 75](/catalog/blind-75), [NeetCode 150](/catalog/neetcode-150), [NeetCode 250](/catalog/neetcode-250), [NeetCode All](/catalog/neetcode).
