Practice Problem

Sum of Two Integers

Difficulty: Medium

Calculate the sum of two integers a and b without using the operators + or -. Use bitwise operations to simulate addition.

Sum of Two Integers

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

Examples

Example 1:

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

Example 2:

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

Example 3:

Input: a = -1, b = 1
Output: 0

Constraints

  • -1000 <= a, b <= 1000

Expected Complexity

  • Time: O(1). at most 32 iterations for 32-bit integers
  • Space: O(1)
MEDIUM
Bit Manipulation
Mathematics
Algorithms
Intermediate

0 views

Solution

Hints

Hint 1
Hint 2
Premium
Hint 3
Premium
Hint 4
Premium