Doing Maths in Binary: How Computers Add
Binary arithmetic follows the same rules as decimal with only two digits to work with. The Binary Calculator adds, subtracts, multiplies and divides, and the mechanics explain a surprising amount about how processors are built.
Addition has four cases
0 plus 0 is 0. 0 plus 1 is 1. 1 plus 0 is 1. And 1 plus 1 is 0 carry 1 - because 2 in binary is written 10.
That is the entire addition table. Adding 1011 and 0110: rightmost column 1 plus 0 is 1. Next, 1 plus 1 is 0 carry 1. Next, 0 plus 1 plus the carry is 0 carry 1. Next, 1 plus 0 plus the carry is 0 carry 1. Result: 10001, which is 17 - and 1011 is 11, 0110 is 6.
The one case that trips people is three ones in a column, which happens when a carry arrives: 1 plus 1 plus 1 is 1 carry 1, since 3 in binary is 11.
Multiplication is shifting and adding
Multiplying by 2 in binary shifts every digit one place left, exactly as multiplying by 10 does in decimal. Dividing by 2 shifts right.
So multiplication is a series of shifts and additions: for each 1 in the multiplier, shift the multiplicand and add. No multiplication table to learn, because the only digits are 0 and 1 - and this is genuinely how multiplier circuits work.
It is also why powers of two appear everywhere in computing. Multiplying or dividing by them is a shift, which is as cheap as an operation gets.
Subtraction by adding a negative
Here is the part that explains a design decision. Processors have no subtraction circuit. They subtract by adding a negative number, using a representation called two's complement.
To negate a binary number: flip every bit, then add 1. In eight bits, 5 is 00000101. Flip to 11111010, add 1, giving 11111011 - which is how -5 is stored. Add that to any number and you have subtracted 5.
The payoff is real hardware simplicity: one adder handles both operations. It also means there is only one representation of zero, which an earlier scheme called one's complement did not manage - it had both a positive and a negative zero, which caused endless trouble.
The side effect is that the range is asymmetric. Eight bits in two's complement span -128 to +127, with one more negative value than positive, and a number exceeding the range wraps silently around to the other end. That is integer overflow, and it is the cause of a whole family of bugs.
Reading binary by sight
The column values are 1, 2, 4, 8, 16, 32, 64, 128. Add up the columns with a 1 in them. 10110 is 16 plus 4 plus 2, which is 22.
Hex is easier for anything long, because one hex digit is exactly four bits - the Number Base Converter covers why.
Common questions
Why do computers use binary?
A circuit distinguishes two states reliably. Distinguishing ten would need far finer tolerances for no benefit.
What is an overflow?
A result too large for the bits available. It wraps rather than erroring, which is why it is dangerous.
How are fractions stored?
Floating point - a sign, an exponent and a fraction. It is why 0.1 plus 0.2 is not exactly 0.3 in most languages.
Got some binary to work on?