ScienceComputer Science

Binary Arithmetic
calculator.

Add, subtract, multiply or divide in base 2, with every carry and borrow shown.

How it's worked out

Binary is base 2. Each column holds a 0 or a 1, and each column is worth twice the one to its right: 1, 2, 4, 8, 16 and so on. You add binary numbers the same way you add decimal ones, column by column from the right. The only change is that a column overflows at 2 instead of 10.

Adding one column
Column holdsTotalWriteCarry
0 + 0000
0 + 1110
1 + 11001
1 + 1 + carry1111

Subtraction also runs right to left. When a column has 0 to take 1 from, it borrows from the next column to the left. That borrow is worth 2 in this column, so 0 − 1 becomes 2 − 1 = 1. If the next column is also 0, the borrow passes on until it reaches a 1.

Multiplication is shift and add. Each 1 in the bottom number copies the top number, moved left by that bit's place; each 0 adds nothing. Add the rows and you have the product. Division runs the other way: bring down one bit at a time, write 1 in the answer when the divisor fits and subtract it, or write 0 when it does not.

The column working draws each method as on paper, one step at a time. The last step checks the answer in decimal: it adds up the place values of the 1 bits and compares them with the same sum done in base 10.

Worked examples

Aisha adds 1011 and 110 for homework

Aisha lines the numbers up on the right, so 110 becomes 0110. Column by column from the right: 1 + 0 = 1, then 1 + 1 = 10, then 0 + 1 + 1 carried = 10, then 1 + 0 + 1 carried = 10. Each 10 means write 0 and carry 1. The last carry becomes a new left-hand digit, so the answer is 10001.

There were 3 carries. To check, 10001 is 16 + 1 = 17, and in decimal 11 + 6 = 17 too.

Open this example in the tool

Ben subtracts 1 from 1000

Ben's right-hand column is 0 − 1, so he must borrow, but the next three columns are 0 as well. The borrow passes through each 0, which then gives 1 to its right, until it reaches the 1 at the left. That makes 3 borrows, and the answer is 111, which is 8 − 1 = 7.

A smaller case is 1010 − 11 = 111, or 10 − 3 = 7. If the second number is the larger, as in 11 − 101, subtract the smaller from the larger and put a minus sign in front: −10.

Open this example in the tool

Chloe multiplies 111 by 111

All three bits of the bottom number are 1, so Chloe writes three rows: 111, 1110, 11100. Each is the top number moved one more place left.

Adding the rows gives 110001. That is 32 + 16 + 1 = 49, the same as 7 × 7 in decimal. A 0 bit in the bottom number gives a row of 0s, so 101 × 11 needs only the rows for its two 1 bits: 1111.

Open this example in the tool

Reading your result

The answer is in binary, with decimal and hex beside it. Hex is base 16, where each digit stands for 4 bits. For division you also get the remainder: 1101 ÷ 100 is 11 with 1 left over, because 3 × 4 = 12 and 12 + 1 = 13.

A carry out of the top column makes the answer one bit longer. 1111 + 1 = 10000: the carry runs through every column. On paper you just write the extra digit.

Real hardware has a fixed width. An 8-bit register cannot hold 11111111 + 1 = 100000000. It keeps the low 8 bits, 00000000, and the lost carry is called overflow/02%3A_Binary_Arithmetic/2.05%3A_Binary_Overflow). The tool says which width holds your answer: 256 needs 16 bits as an unsigned number. It works to 256 digits, so even 64 ones plus 1 is exact, a 1 followed by 64 zeros.

Negative numbers are written with a minus sign here. Computers usually store them in two's complement/02%3A_Binary_Arithmetic/2.03%3A_Negative_Binary_Numbers) instead: flip every bit and add 1. Then subtracting is just adding the negative and dropping the extra carry bit.

Common mistakes

  • Writing 2 in a column. 1 + 1 is 10 in binary, so you write 0 and carry 1. A 2 or a 3 never appears in a binary answer.
  • Forgetting the carry when it meets 1 + 1. A column with 1 + 1 and a carry is 11: write 1 and carry 1 again.
  • Borrowing only one column away. In 1000 − 1 the borrow must pass through every 0 until it reaches a 1, and each 0 it passes becomes 1.
  • Not lining up on the right. 1011 + 110 means 1011 + 0110. Lining the numbers up on the left adds the wrong place values.
  • Skipping a row for a 0 bit when multiplying. The rows still move left for each place, so a 0 in the middle of the bottom number shifts the next row two places.

Questions people ask

The total is 3, which is 11 in binary, so you write 1 in that column and carry 1 to the next. A column can never carry more than 1 when you add two numbers.

Sources