v9.1.1
Menu

Sum of Two Integers

mediumFREEBit ManipulationBlind 75

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

Example

Input
1
2
Output
3
Reasoning
  • Inputs: a and b are ordinary signed integers, each between −1000-1000 and 10001000. Here a =1= 1 and b =2= 2.
  • Output: a single integer equal to a+ba + b, so the sample answer is 1+2=31 + 2 = 3. The challenge is producing it without the plus or minus operators.
  • The math in base 2: 1=0121 = 01_2 and 2=1022 = 10_2. Adding column by column, the ones column holds 1+0=11 + 0 = 1 and the twos column holds 0+1=10 + 1 = 1. No column ever sums to 22, so nothing spills into the next column, and the result is 112=21+20=311_2 = 2^1 + 2^0 = 3.
  • Notice: in a case like 2+32 + 3 (102+11210_2 + 11_2) the twos column holds two 1s, which spills a 1 into the fours column and gives 1012=5101_2 = 5.
  • Signs matter: for −1+1-1 + 1 the answer must be exactly 0, and a negative pair such as −5+3-5 + 3 must come back as −2-2, a genuine negative integer.

Constraints

  • -1000 <= a, b <= 1000

More from Blind 75

solution.py

Test results

Sign in to run your code against the test cases.