CodeSpeek

Sum of Two Integers

Medium · Bit Manipulation

You are given two integers. Return their sum, but you may not use the + or - operators (or any built-in sum/subtract helpers) to compute it. Think in terms of bitwise operations to combine the values instead.

Examples

Input:  a = 1, b = 2
Output: 3
Why:    1 plus 2 equals 3, computed without using + or -.
Input:  a = 2, b = 3
Output: 5
Why:    2 plus 3 equals 5.
Input:  a = -2, b = 3
Output: 1
Why:    Adding a negative and a positive number gives 1.

Constraints

-1000 <= a, b <= 1000

Practise it by voice

Describe the solution out loud and the interviewer writes exactly what you say, asks when you are vague, and runs the tests in your browser.

Practise Sum of Two Integers

This statement is written for CodeSpeek. The problem is part of the NeetCode 150 list; Watch NeetCode's explanation of Sum of Two Integers. Reference solutions from the NeetCode repository (MIT) are used to verify our tests.