Sum of Two Integers
Add two numbers with bit operations alone, by splitting each addition into the bits it leaves and the carries it makes.
Given two integers ab+-
Example 1
Output: 3
Example 2
Output: 5
Example 3
Output: -3
The Idea
When you add 58 and 67 on paper, you go one column at a time. The ones column gives 8 + 7 = 15, so you write 5 and carry 1 into the tens column. Each column does two jobs: it leaves a digit behind, and it may push a carry one place to the left.
Computers add the same way, only in binary, where every column holds a single 0 or 1, called a bit. In Example 2, 2 is written 010011
In binary, 1 + 1 makes 2, which is 10+
Look at the column jobs one by one. The bit a column keeps, if we ignore carries, is 1 when exactly one of the two bits is 1. Java's ^2 ^ 3001
A carry, on the other hand, starts only where both bits are 1. Java's &2 & 3010<< 1100
The picture shows these numbers for Example 2. In Round 1, a = 2 and b = 3 both have a 1 at position 1, marked in orange. The row "sum without carries (a ^ b)" keeps only the 1 at position 0, worth 1. The row "carries, one place left" holds the carry at position 2, also in orange, worth 4.
The answer is now 1 + 4, the sum without carries plus the carries, and that is another addition. So Round 2 treats them the same way, with a = 1 and b = 4. They have no 1s in the same position, so the sum without carries is 101
So one addition turns into a few rounds of XOR and AND. Each round passes its carries on to the next, and the rounds stop when one of them makes no carries.
Solution Approach
Three things decide the code: what one round computes, why the rounds always stop, and what happens with negative numbers and with sums too big for an int
One round with XOR and AND
Why does a round keep the total the same? A position where only one number has a 1 keeps that 1 in a ^ ba + b(a ^ b) + ((a & b) << 1)
So each round may replace ababcarrya
When the rounds stop
How many rounds can it take? The carries are made from positions where bbintb
Example 2 needs two rounds, as the picture shows. The slowest case is a carry that has to travel through every position. Adding -1 and 1 is such a case, and a run that counts rounds shows it takes all 32.
Negative numbers and overflow
How does a Java int11111111111111111111111111111001111
Because negative numbers are stored so that ordinary column addition works on them, the loop needs no special case. In Example 3, -7 ends in ...1100100100...11101
What if the true sum does not fit in an intInteger.MAX_VALUEInteger.MIN_VALUE++
Java Solution
The method getSum
- Loop while
is not 0. At the startb is the second number; after each round it holds the carries still to be added.b - Inside the loop, first work out the carries from the current values,
.carry = (a & b) << 1 - Then set
toa , the sum without carries.a ^ b - Then set
tob , so the next round adds those carries.carry - Once
is 0, nothing is left to carry, andb holds the sum, so the method returnsa .a
A small helper, showmainint
public class SumOfTwoIntegers {
public static int getSum(int a, int b) {
while (b != 0) {
int carry = (a & b) << 1; // where both bits are 1, a carry goes one place left
a = a ^ b; // the bits added with no carries
b = carry; // the carries still to be added
}
return a;
}
static void show(int a, int b) {
System.out.println(a + " + " + b + " = " + getSum(a, b));
}
public static void main(String[] args) {
show(1, 2);
show(2, 3);
show(-7, 4);
show(-1, 1);
show(0, 0);
show(Integer.MAX_VALUE, 1);
show(Integer.MIN_VALUE, -1);
}
}
Running it prints:
1 + 2 = 3
2 + 3 = 5
-7 + 4 = -3
-1 + 1 = 0
0 + 0 = 0
2147483647 + 1 = -2147483648
-2147483648 + -1 = 2147483647
Look at -7 + 4 = -3-1 + 1 = 00 + 0b
The last two lines wrap around. 2147483647 + 1-2147483648 + -1+
The shift in (a & b) << 1carry = a & bgetSum(2, 3)
Complexity
Each round does three bit operations, and the 32 bits of an int
- Time: O(1), because the lowest 1 in
climbs at least one position per round, so no input needs more than 32 rounds, and -1 + 1 needs exactly 32.b - Space: O(1), because the loop only rewrites
anda and keeps one extrab ,int .carry