{x}
blog image

Sum of Two Integers

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

 

Example 1:

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

Example 2:

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

 

Constraints:

  • -1000 <= a, b <= 1000

Solution Explanation for Sum of Two Integers

This problem challenges you to add two integers without using the + or - operators. The key is to understand binary addition at a bit level and leverage bitwise operators.

Core Idea: Binary addition works by adding bits individually, considering carry-overs. We can simulate this using bitwise XOR (^) for the sum without carry and bitwise AND (&) for the carry bits.

Algorithm:

  1. Bitwise XOR (^): This operator gives you the sum of two bits without considering the carry. For example, 1 ^ 1 = 0 (with a carry of 1), and 1 ^ 0 = 1.

  2. Bitwise AND (&): This operator identifies the bits where a carry is generated. A carry occurs only when both bits are 1 (1 & 1 = 1). We left-shift (<< 1) the result to position the carry in the correct place for the next addition.

  3. Iteration: We repeat steps 1 and 2 until there are no more carry bits (i.e., b becomes 0).

Handling Overflow (for larger integers):

The provided solutions account for potential integer overflow, especially in languages like Python and Java, by using bitwise AND with 0xFFFFFFFF to ensure results remain within the bounds of a 32-bit unsigned integer. The final check in Python (return a if a < 0x80000000 else ~(a ^ 0xFFFFFFFF)) handles potential negative results.

Code Explanation (Python):

class Solution:
    def getSum(self, a: int, b: int) -> int:
        a, b = a & 0xFFFFFFFF, b & 0xFFFFFFFF  # Handle potential overflow
        while b:
            carry = ((a & b) << 1) & 0xFFFFFFFF # Calculate carry, handle overflow
            a, b = a ^ b, carry                # Update a (sum without carry) and b (carry)
        return a if a < 0x80000000 else ~(a ^ 0xFFFFFFFF) # Handle negative results
  • a & 0xFFFFFFFF and b & 0xFFFFFFFF: This ensures both a and b are treated as 32-bit unsigned integers. This prevents unexpected behavior with negative numbers.

  • while b:: The loop continues as long as there are carry bits (b is not zero).

  • carry = ((a & b) << 1) & 0xFFFFFFFF: This calculates the carry bits. The << 1 shifts the carry bits one position to the left.

  • a, b = a ^ b, carry: a becomes the sum without carry, and b becomes the new carry bits for the next iteration.

  • return a if a < 0x80000000 else ~(a ^ 0xFFFFFFFF): This final check handles potential negative results by converting the unsigned 32-bit integer back to a signed integer if necessary. 0x80000000 is the minimum value of a 32-bit signed integer.

Time and Space Complexity:

  • Time Complexity: O(1). The maximum number of iterations is 32 (the number of bits in a 32-bit integer). This is considered constant time.
  • Space Complexity: O(1). The algorithm uses a fixed amount of extra space regardless of the input values.

The Java, C++, and Go code examples follow the same logic, with minor adjustments to handle language-specific aspects of integer representation and overflow. The recursive solution in Java is functionally equivalent to the iterative approach in other languages.