easy +10 pts

Add without plus

Implement integer addition using only bitwise operations and no '+' or '-' operators.

Write a function `add_without_plus(a: int, b: int) -> int` that returns the sum of two integers `a` and `b` WITHOUT using the `+` or `-` operators (and no built-in `sum` or similar). You may use bitwise operators (`&`, `|`, `^`, `~`, `<<`, `>>`) and loops/conditionals. The inputs will be integers in the range `-1000 <= a, b <= 1000`. The result will fit within the standard Python integer range. For example, `add_without_plus(3, 5)` should return `8`, `add_without_plus(-2, 7)` should return `5`, and `add_without_plus(0, 0)` should return `0`. **Important:** Your solution must work for negative integers as well. You may assume Python's arbitrary-precision integers, but you cannot use `+`, `-`, `sum`, or any other arithmetic operator that implicitly does addition/subtraction (e.g., `-` unary is also disallowed). The solution should rely on bit manipulation to compute the sum.

Constraints

- `-1000 <= a, b <= 1000` - Output fits in standard integer range (within ±2000). - Time complexity: O(1) expected (at most a fixed number of bit iterations, e.g., 32 or 64). - Space: O(1).

Example

>>> add_without_plus(3, 5)
8
>>> add_without_plus(-2, 7)
5
>>> add_without_plus(0, 0)
0
>>> add_without_plus(-5, -7)
-12
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Recall that XOR (`^`) gives the sum of two bits without carry.
The carry from the current bit position can be computed using AND (`&`) and shifting left (`<< 1`).
Iterate until the carry becomes zero, repeatedly updating the sum and carry variables.
Negative numbers are represented in two's complement; using a fixed-width mask (e.g., 32 bits) and converting back to a signed integer handles them cleanly.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.