Binary calculator
Add, subtract, multiply and divide binary numbers and see the working the way it is done on paper: carries above the columns, borrows, partial products and long division. AND, OR, XOR, NOT and shifts are here too, at any width.
Working
| carries | 1 | 1 | 1 | 1 | 1 | 1 | |
|---|---|---|---|---|---|---|---|
| first number | 1 | 0 | 1 | 1 | 0 | 1 | |
| second number | 1 | 1 | 0 | 1 | 1 | ||
| sum | 1 | 0 | 0 | 1 | 0 | 0 | 0 |
1 carried into the column below it
- Column 1 (2⁰): 1 + 1 = 10. Write 0, carry 1.
- Column 2 (2¹): 0 + 1 + 1 carried = 10. Write 0, carry 1.
- Column 3 (2²): 1 + 0 + 1 carried = 10. Write 0, carry 1.
- Column 4 (2³): 1 + 1 + 1 carried = 11. Write 1, carry 1.
- Column 5 (2⁴): 0 + 1 + 1 carried = 10. Write 0, carry 1.
- Column 6 (2⁵): 1 + 0 + 1 carried = 10. Write 0, carry 1.
- The last carry becomes a new digit on the left.
Binary addition rules
Binary adds column by column exactly like decimal, but a column carries as soon as it reaches two. With a carry coming in from the right, each column adds up to three bits, so there are only eight cases:
| Carry in | A | B | Total | Write | Carry out |
|---|---|---|---|---|---|
| 0 | 0 | 0 | 0 + 0 + 0 = 0 | 0 | 0 |
| 0 | 0 | 1 | 0 + 0 + 1 = 1 | 1 | 0 |
| 0 | 1 | 0 | 0 + 1 + 0 = 1 | 1 | 0 |
| 0 | 1 | 1 | 0 + 1 + 1 = 10 | 0 | 1 |
| 1 | 0 | 0 | 1 + 0 + 0 = 1 | 1 | 0 |
| 1 | 0 | 1 | 1 + 0 + 1 = 10 | 0 | 1 |
| 1 | 1 | 0 | 1 + 1 + 0 = 10 | 0 | 1 |
| 1 | 1 | 1 | 1 + 1 + 1 = 11 | 1 | 1 |
The two to remember are 1 + 1 = 10 (write 0, carry 1) and 1 + 1 + 1 = 11 (write 1, carry 1). Here is 1011 + 111, which is 11 + 7 = 18 in decimal:
| carries | 1 | 1 | 1 | 1 | |
|---|---|---|---|---|---|
| first number | 1 | 0 | 1 | 1 | |
| second number | 1 | 1 | 1 | ||
| sum | 1 | 0 | 0 | 1 | 0 |
- Column 1 (2⁰): 1 + 1 = 10. Write 0, carry 1.
- Column 2 (2¹): 1 + 1 + 1 carried = 11. Write 1, carry 1.
- Column 3 (2²): 0 + 1 + 1 carried = 10. Write 0, carry 1.
- Column 4 (2³): 1 + 0 + 1 carried = 10. Write 0, carry 1.
- The last carry becomes a new digit on the left.
That table is the truth table of a full adder, and the column by column method is exactly what a ripple carry adder does in hardware: one full adder per column, each passing its carry to the next, which is why the carry has to ripple from right to left before the top bit is ready.
Binary subtraction with borrows
Subtract column by column from the right. 1 − 0 = 1, 1 − 1 = 0 and 0 − 0 = 0. The only hard case is 0 − 1: borrow from the column to the left, which turns this column's 0 into 10 (two), and 10 − 1 = 1. The column that lent is then one less. Here is 1100 − 101, or 12 − 5 = 7:
| borrows | 1 | 1 | 1 | |
|---|---|---|---|---|
| first number | 1 | 1 | 0 | 0 |
| second number | 1 | 0 | 1 | |
| difference | 1 | 1 | 1 |
1 marks a column that lent 1 to the column on its right.
- Column 1 (2⁰): 0 − 1 is below zero, so borrow 10 from the next column: 1. Write 1.
- Column 2 (2¹): 0 − 1 (lent) − 0 is below zero, so borrow 10 from the next column: 1. Write 1.
- Column 3 (2²): 1 − 1 (lent) − 1 is below zero, so borrow 10 from the next column: 1. Write 1.
- Column 4 (2³): 1 − 1 (lent) − 0 = 0. Write 0.
When the second number is larger, work out the difference the other way round and put a minus sign in front. A processor does not do that: it adds the two's complement of the second number, so the same adder does both jobs, and the answer comes out as a two's complement pattern.
Multiplication is shift and add
Multiplying by a single bit is trivial: times 1 is the number itself, times 0 is nothing. So binary long multiplication is a list of shifted copies of the first number, one for each 1 in the second, added together. Here is 1011 × 101, or 11 × 5 = 55:
| first number | 1 | 0 | 1 | 1 | ||
|---|---|---|---|---|---|---|
| second number | 1 | 0 | 1 | |||
| 1011 × 1, shifted 0 | 1 | 0 | 1 | 1 | ||
| 1011 × 0, shifted 1 | 0 | 0 | ||||
| 1011 × 1, shifted 2 | 1 | 0 | 1 | 1 | 0 | 0 |
| product | 1 | 1 | 0 | 1 | 1 | 1 |
- Bit 0 of the multiplier is 1: write 1011.
- Bit 1 of the multiplier is 0: nothing to add, a row of zeros.
- Bit 2 of the multiplier is 1: write 1011, shifted 2 places left.
- Add the rows to get 110111.
Shifting left by one place doubles a binary number, the way adding a 0 multiplies a decimal number by ten. The dimmed zeros above are those shifts.
Binary long division
Division works like decimal long division, but each quotient digit is 0 or 1: either the divisor fits into the bits brought down so far or it does not. Here is 110101 ÷ 101, or 53 ÷ 5 = 10 remainder 3:
| Bring down | Now | 101 goes in | Take away | Left |
|---|---|---|---|---|
| 1 | 1 | 0 | 0 | 1 |
| 1 | 11 | 0 | 0 | 11 |
| 0 | 110 | 1 | 101 | 1 |
| 1 | 11 | 0 | 0 | 11 |
| 0 | 110 | 1 | 101 | 1 |
| 1 | 11 | 0 | 0 | 11 |
The quotient is the "goes in" column read downwards, 1010, and the remainder is what is left at the end, 11.
Overflow in a fixed width
On paper a sum can always grow a digit. In a register it cannot: an 8 bit register holds 8 bits, and a carry out of the top column is lost. Set the width to 8 bits and add 11001000 + 1100100 (200 + 100):
| carries | 1 | 1 | |||||||
|---|---|---|---|---|---|---|---|---|---|
| first number | 1 | 1 | 0 | 0 | 1 | 0 | 0 | 0 | |
| second number | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 | |
| sum | 1 | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 0 |
The true answer, 300, needs 9 bits. The register keeps the low 8, 00101100, which is 44. Subtraction wraps the other way: in 8 bits, 11 − 101 (3 − 5) leaves 11111110, which is 254 read as unsigned and −2 read as a signed two's complement number. The bits are the same; only the reading differs.
The signed reading is only right while the true answer fits. Eight signed bits reach down to −128, so 0 − 11111111 (0 − 255 = −255) leaves 00000001, which reads as 1 either way. That is a signed overflow: the bits cannot hold the answer at all.
Bitwise operations
AND, OR and XOR work on each column separately, with no carries, following the logic gates of the same names. AND with a mask keeps only the bits you want: here the mask 00001111 keeps the low four bits.
| first number | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| second number | 0 | 0 | 0 | 0 | 1 | 1 | 1 | 1 |
| result | 0 | 0 | 0 | 0 | 0 | 1 | 1 | 0 |
NOT flips every bit, so its answer depends on how many bits there are: pick a width, or the calculator uses the number of digits you typed. A left shift by n multiplies by 2n, and a right shift divides by 2n and drops the remainder.
Questions
How do you add binary numbers?
Line them up on the right and add column by column, as in decimal, but carry at 2 instead of 10. 0 + 0 = 0, 0 + 1 = 1, 1 + 1 = 10 (write 0, carry 1), and 1 + 1 + 1 = 11 (write 1, carry 1). The calculator above shows every carry above its column.
How do you subtract binary numbers?
Column by column from the right. 1 − 0 = 1, 1 − 1 = 0 and 0 − 0 = 0. For 0 − 1, borrow from the next column to the left: the 0 becomes 10 (two), 10 − 1 = 1, and the column you borrowed from is one less. If the bottom number is bigger, subtract the other way round and put a minus sign on the answer. Computers instead add the two's complement, which gives the same bits.
How do you multiply binary numbers?
For every 1 in the second number, write the first number shifted left by that bit's position; for every 0, write nothing. Then add the rows. That is shift and add, and it is how simple processors multiply: 1011 × 101 is 1011 plus 1011 shifted two places, 1011 + 101100 = 110111.
How do you divide binary numbers?
Long division, which is easier than in decimal because each quotient digit is 0 or 1: either the divisor fits into what you have so far, and you subtract it and write 1, or it does not, and you write 0 and bring down the next bit. What is left at the end is the remainder.
What is overflow?
In a fixed number of bits, an answer that needs more bits than there are. Adding 1 to 11111111 in eight bits gives 00000000 with a carry out that has nowhere to go. For unsigned numbers that carry out is what signals overflow, and a processor records it in the carry flag (C or CF). For signed two's complement numbers overflow is when two numbers of the same sign add up to the opposite sign, and that is what the overflow flag (V or OF) records.
How long can the numbers be?
Up to 64 bits each. With the width set to Unlimited, answers grow as long as they need to; with a fixed width of 8, 16, 32 or 64 bits the result wraps round the way a register does, and the calculator says when that happened.
Can it handle negative numbers?
Inputs are unsigned. A subtraction that goes below zero shows the answer with a minus sign in Unlimited width, and in a fixed width it shows the wrapped bit pattern, which is the two's complement of the negative answer. The two's complement page explains how that encoding works.