Show problems as
Classic problem #371MediumMathBit Manipulation

Sum of Two Integers

Real-world version: Bitwise ALU Total
Tracked in this browser only
Read Sum of Two Integers on LeetCode(opens in a new tab)

The statement here is our own: the same inputs, outputs and technique.

You are writing microcode for a teaching processor whose arithmetic unit was left off the die. The only opcodes available to you are bitwise AND, OR, XOR, NOT, left shift and right shift, plus comparisons and loops.

Two signed sensor readings a and b arrive in registers. Report their combined value.

Your routine must not apply the + or - operators anywhere, and it must not reach for a library routine that performs arithmetic on your behalf. Readings may be negative; the machine stores a register as a 32-bit two's complement word, and every legal total fits in such a word.

Examples

Example 1

Input
a = 14, b = 9
Output
23

The two readings come to 23 together.

Example 2

Input
a = -7, b = 4
Output
-3

Readings may be negative; sitting 4 above -7 lands on -3.

Example 3

Input
a = -18, b = -25
Output
-43

Both registers hold negative readings, and together they come to -43.

Example 4

Input
a = 640, b = -640
Output
0

The two readings cancel, so the routine reports 0.

Constraints

  • -1000 <= a, b <= 1000

The signature

The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.

Python
def get_sum(a: int, b: int) -> int:
Java
public int getSum(int a, int b)