Information Technology Topic

Binary Addition and Subtraction

  1. Read notes
  2. Flashcards
  3. Quiz
  4. Confident

Study focus - 3 things

  1. Binary addition works column by column from the right with four cases: 0+0=0, 0+1=1, 1+1=10 (write 0, carry 1) and 1+1+1=11 (write 1, carry 1)
  2. Binary subtraction also works from the right; when you must take 1 from 0 you borrow 1 from the next column to the left, so the 0 becomes 10
  3. A fixed-size register keeps only its own bits, so a sum that needs an extra bit overflows (for 8 bits, anything above 255)

Section 1The four addition cases

Binary addition is decimal addition with only two digits. The sum of a column can only be 0, 1, 2 or 3, and each of those is written in binary: 0 + 0 = 0 0 + 1 = 1 (and 1 + 0 = 1) 1 + 1 = 10, so write 0 and carry 1 to the next column 1 + 1 + 1 = 11, so write 1 and carry 1 (two bits plus a carry) Here's the trick: when a column adds to 2 or more, write the units part and carry the 'twos' part. That is exactly what you do in decimal when a column adds to ten or more.

Section 2Adding in columns

Line the numbers up with the right-hand digits under each other. Start at the rightmost column and move left. Each carry goes above the next column. Example, 0101 + 0011 (5 + 3): Carry | 1 | 1 | 1 | 0 A | 0 | 1 | 0 | 1 B | 0 | 0 | 1 | 1 Sum | 1 | 0 | 0 | 0 Right column: 1 + 1 = 10, write 0 carry 1. Next: 0 + 1 + carry 1 = 10, write 0 carry 1. Next: 1 + 0 + carry 1 = 10, write 0 carry 1. Left: 0 + 0 + carry 1 = 1. The sum is 1000, which is 8. The carry row shows the carry coming into each column.

Section 3A longer sum, and checking it

Add 00101101 (45) and 00011011 (27): Carry | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 A | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 B | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 Sum | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 The answer is 01001000. Check by converting to decimal: 64 + 8 = 72, and 45 + 27 = 72. Checking in decimal is the quickest way to catch a missed carry. Convert both numbers, add them in decimal, and compare with the decimal value of your binary answer.

Section 4Overflow: when the answer does not fit

A computer stores numbers in a fixed number of bits. If the sum needs one more bit than is available, the extra bit is lost and the stored answer is wrong. This is overflow. Example in 8 bits, 11001000 + 01100100 (200 + 100): The sum is 100101100, which needs 9 bits (it is 300). Only the right-hand 8 bits are kept: 00101100, which is 44. The largest value 8 bits can hold is 255, so two numbers whose sum is over 255 will overflow. Watch out: an answer that is smaller than one of the numbers you added is a sign something has overflowed.

Section 5Binary subtraction and borrowing

Subtract column by column from the right. The cases are 0 − 0 = 0, 1 − 0 = 1 and 1 − 1 = 0. The tricky one is 0 − 1. When you try to take 1 from 0, you borrow 1 from the next column to the left. That column loses one lot of its value and your 0 becomes 10 (two), so 10 − 1 = 1. If the next column to the left is also 0, it has to borrow from the one beyond it, and the borrow passes along until a column with a 1 is found. It works just like borrowing across zeros in decimal, such as 1000 − 1.

Section 6A worked subtraction

Subtract 0110 from 1101 (13 − 6): Lent a 1 | 1 | 1 | 0 | 0 Top | 1 | 1 | 0 | 1 Minus | 0 | 1 | 1 | 0 Answer | 0 | 1 | 1 | 1 Column 1 (right): 1 − 0 = 1. Column 2: 0 − 1 cannot be done, so borrow from column 3, so the 0 becomes 10 and 10 − 1 = 1. Column 3 has lent its 1, so it holds 0; it needs to take 1 away, so it borrows from column 4 and 10 − 1 = 1. Column 4 has lent its 1 and holds 0; 0 − 0 = 0. The answer is 0111, which is 7. Check by adding: the answer plus the number you subtracted must equal the number you started with. 0111 + 0110 = 1101.

Section 7Another way to subtract

Subtraction can also be done by complement-and-add: instead of taking a number away, you add its negative. Computers use this because one adding circuit can then do both jobs. That method needs negative numbers in binary, so it is taught in the topic on signed integers and two's complement. For this topic, borrowing is the method to use in an exam when you are asked to subtract binary integers by hand. All numbers here are whole numbers and small enough for 8 bits.

Examples

At a call centre in Montego Bay, 45 calls were answered in the morning and 27 in the afternoon. The totals are held as 8-bit binary numbers: 00101101 and 00011011. (a) Add the two binary numbers, showing the carries. (b) Check your answer by converting to decimal. (c) State whether the sum fits in 8 bits.

(a) Carry | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 A | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 B | 0 | 0 | 0 | 1 | 1 | 0 | 1 | 1 Sum | 0 | 1 | 0 | 0 | 1 | 0 | 0 | 0 Working from the right: 1 + 1 = 10 (write 0, carry 1); 0 + 1 + 1 = 10 (write 0, carry 1); 1 + 0 + 1 = 10 (write 0, carry 1); 1 + 1 + 1 = 11 (write 1, carry 1); 0 + 1 + 1 = 10 (write 0, carry 1); 1 + 0 + 1 = 10 (write 0, carry 1); 0 + 0 + 1 = 1. The answer is 01001000. (b) 01001000 = 64 + 8 = 72, and 45 + 27 = 72. The two agree. (c) 72 is less than 255 and the carry out of the leftmost column is 0, so there is no overflow.

A shop in Mandeville holds 100 items, stored as the 8-bit number 01100100. It sells 45 items, stored as 00101101. (a) Subtract 00101101 from 01100100 using borrowing. (b) Check the answer by adding. (c) Explain what would go wrong if the shop tried to subtract 45 from 100 by adding instead.

(a) Lent a 1 | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 0 Top | 0 | 1 | 1 | 0 | 0 | 1 | 0 | 0 Minus | 0 | 0 | 1 | 0 | 1 | 1 | 0 | 1 Answer | 0 | 0 | 1 | 1 | 0 | 1 | 1 | 1 The borrows pass along the row of zeros from the right, so every column except the leftmost and rightmost lends a 1. The answer is 00110111, which is 32 + 16 + 4 + 2 + 1 = 55. (b) Add the answer and the number subtracted: 00110111 + 00101101 = 01100100, which is the 100 we started with. Decimal check: 100 − 45 = 55. (c) Adding 00101101 to 01100100 gives 10010001, which is 145. Adding makes the stock bigger, not smaller, so the operation must be subtraction.

Now let's water it

Session runs notes to flashcards to quiz back-to-back.

Where this comes from