Sum of Two Integers
Add two integers without using + or -
DSA, Medium, Bit Manipulation
Which structure or algorithm, and what does it cost.
A tiny chip has no adder. It can only do bit operations: and, or, xor and shifts. Using only those, add two whole numbers a and b and return the sum. Do not use the + or - operator on the numbers. A plain loop counter is allowed. Treat both numbers as 32-bit signed values in two's complement. In a language with no fixed integer width, such as Python, your code must apply the 32-bit rule itself.
Examples
Constraints
aandbare whole numbers from -30,000 to 30,000.- The sum of two such numbers fits in 32 bits, so no result wraps around.
Your answer must contain
- The simple method and its cost; a method that meets the target cost and why it ends; how you apply the 32-bit rule; code; time and memory cost; a hand trace of example 1.
Target Aim for at most 32 rounds, so O(1) time, and O(1) memory.
Hints
Try the problem first. Open one hint at a time.
Hint 1 a nudge
Think about adding in binary on paper, column by column. What do you write down, and what do you carry?
Hint 2 the approach
Without carries, each column is the xor of the two bits. The carries come from another operation, and each lands one column higher. What if you add them in again?
Hint 3 the key step
Xor gives each column's sum without carries. Another operation finds the carries. Adding the two is the same problem again. When does it stop? What keeps it in 32 bits?
Walkthrough
Full walkthrough brute force to best
Brute force
Add one column at a time, as on paper. For each of the 32 positions, take the two bits and the carry in. The sum bit is the xor of the three, and the carry out is 1 when at least two of the three are 1. This always runs 32 rounds, so it is O(1) time and O(1) memory. It is correct, but it needs a carry variable and builds the answer bit by bit.
Best method
Do all columns at once. a xor b is the sum of every column with the carries left out. a and b, shifted left by one, is every carry placed one column up. The true sum is those two numbers added, which is the same problem with a smaller carry. Repeat until the carry is 0. Each round the lowest 1 bit of the carry moves up at least one place, so after at most 32 rounds the carry is 0. Python integers never overflow, so mask both numbers to 32 bits each round, and turn a final value above 2^31 - 1 back into a negative number.
Hand trace
Run on a = 13 and b = 29. Round 1: a is 01101 and b is 11101. Xor is 10000. The carry is and, which is 01101, shifted left, 11010. Round 2: a is 10000 and b is 11010. Xor is 01010. The carry is 10000 shifted left, 100000. Round 3: a is 01010 and b is 100000. Xor is 101010. The carry is 0, so stop. The result is 101010, which is 42.
Code
def add_without_plus(a, b):
mask = 0xFFFFFFFF
a &= mask
b &= mask
while b:
a, b = (a ^ b) & mask, ((a & b) << 1) & mask
return a if a <= 0x7FFFFFFF else ~(a ^ mask)Time and memory
At most 32 rounds, each a few bit operations, so O(1) time in the 32-bit model. O(1) memory.
Common mistakes
- Skipping the 32-bit mask in Python. A negative number has endless leading 1 bits, so the carry never reaches 0 and the loop never ends.
- Forgetting to shift the carry left by one. A carry belongs one column higher.
- Returning the masked value as it is. A value above 2^31 - 1 stands for a negative number and must be converted back.
- Doing one round only. A carry can make a new carry, so the step must repeat until the carry is 0.