Sum of Two Integers
Real-world version: Bitwise ALU TotalThe 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.
def get_sum(a: int, b: int) -> int:public int getSum(int a, int b)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 4 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's