All problems

Sum of Two Integers

Medium · bit manipulation

You are given two 32-bit signed integers a and b. Return their sum a + b, but do the addition without using the + or - operators on the numbers (a loop counter is fine). Use bitwise operations instead.

Negative numbers are stored in two's complement, as in a real 32-bit machine. The true sum of a and b always fits in a signed 32-bit integer, so you never need to worry about overflow in the answer.

Examples

Example 1

Input: a = 1, b = 2

Output: 3

Example 2

Input: a = -2, b = 3

Output: 1

A negative and a positive number: the result is 3 - 2 = 1.

Example 3

Input: a = -5, b = -7

Output: -12

Two negative numbers add up to a negative number.

Constraints