1.1 Number systems

Show All Section Notes
Test yourself on Number systems 11 questions — drag-to-order, code completion, matching and multiple choice.
Start the quiz

Two's Complement and Binary Subtraction

Watch this lesson Video 1.1.6 · 13:06 · Negative numbers in eight bits, the shortcut, and subtracting by adding

So far every binary number has been positive. To store negative numbers, the 256 patterns of an 8-bit register have to be shared between positive and negative values. One’s complement is the obvious first attempt and it has two flaws. Two’s complement fixes both, and it lets a computer subtract by adding. There is also a shortcut that turns the two-step conversion into one pass.

1. The problem: where does the minus sign go?

A register holds only 0s and 1s. There is no space for a “−” symbol, so the sign has to be shown by the bits themselves. Both systems below use the same idea: if the most significant bit (MSB) is 1, the number is negative. They differ in how the remaining bits are worked out.

2. One’s complement

Definition In one’s complement, a negative number is made by inverting every bit of the positive number: every 0 becomes 1 and every 1 becomes 0.
+18 0001 0010 -18 1110 1101 every bit flipped. The MSB is now 1, so the number is negative

It is easy to do, but it causes two problems:

Two zeros

0000 0000 is +0, and inverting it gives 1111 1111, which is −0. One of the 256 patterns is wasted on a duplicate. Every comparison with zero also has to check for both patterns.

Range in 8 bits: −127 to +127.

Addition needs a fix-up step

When adding a negative number produces a carry out of the MSB, that carry has to be added back on at the right-hand end (an end-around carry) before the answer is correct. That means a second addition, and extra circuitry.

One's complement: 5 + (−3) 1 1111 1 carries 0000 0101 +5 + 1111 1100 −3 (3 = 0000 0011, inverted) ---------- 1 0000 0001 carry out of the MSB add the carry back on at the right ("end-around carry") + 0000 0001 ---------- 0000 0010 = +2, but only after a second addition

3. Two’s complement

Definition In two’s complement, a negative number is made by inverting every bit of the positive number (the one’s complement) and then adding 1. Equivalently, the MSB has a place value of −128 instead of +128.
The most significant bit is worth −128. Every other column is positive. Place value Bits Counts −128 1 −128 64 1 +64 32 1 +32 16 0 8 1 +8 4 1 +4 2 1 +2 1 0 = −18 −128 + 64 + 32 + 8 + 4 + 2 = −18. A 1 in the MSB means the number is negative.
Figure 1 — Reading 1110 1110 using place values. Only the MSB column is negative.

Adding 1 after inverting removes the duplicate zero. Inverting 0000 0000 gives 1111 1111, and adding 1 gives 1 0000 0000. The 9th bit is discarded, leaving 0000 0000 again. There is now only one zero, so the spare pattern holds an extra negative number: 1000 0000 is −128.

8-bit patternUnsignedOne’s complementTwo’s complement
0111 1111127+127+127  (largest)
0000 00011+1+1
0000 00000+00
1111 1111255−0−1
1111 1110254−1−2
1000 0001129−126−127
1000 0000128−127−128  (smallest)

Converting denary to 8-bit two’s complement

  1. Write the positive version of the number in 8-bit binary, padded with leading 0s. (If the number is positive, stop here. The MSB must be 0.)
  2. For a negative number, invert every bit.
  3. Add 1. Ignore any carry out of the MSB.

Converting 8-bit two’s complement to denary

  • MSB is 0: the number is positive. Read it as ordinary binary.
  • MSB is 1: the number is negative. Either add up the place values using −128 for the MSB (Figure 1), or find its two’s complement again to get the positive size, then put a minus sign in front.
Why two’s complement won It has one zero, it holds one more value (−128 to +127), and ordinary binary addition gives the right answer for any mix of positive and negative numbers with no fix-up step: a carry out of the MSB is simply thrown away. Compare the two’s complement sum below with the one’s complement sum in section 2.
Two's complement: 5 + (−3) 1 1111 1 1 carries 0000 0101 +5 + 1111 1101 −3 (3 = 0000 0011, invert, add 1) ---------- 1 0000 0010 carry out of the MSB 0000 0010 discard the carry: 0000 0010 = +2, done

4. The shortcut

Look closely at what happens when you invert and then add 1. The figures below find −104 both ways.

Method 1 — invert every bit, then add 1 Start with +104 0 1 1 0 1 0 0 0 = 104 Invert every bit 1 0 0 1 0 1 1 1 one's complement Add 1 0 0 0 0 0 0 0 1 Result 1 0 0 1 1 0 0 0 = −104 + Green: adding 1 ripples through the 1s and stops at the first 0, so these bits end up exactly as they started. Amber: the carry never reaches these bits, so they stay inverted.
Figure 2 — After adding 1, the green bits are back to exactly what they were in the original number.

The observation: the original number ends in 1000, its first 1 from the right followed by zeros. Inverting turns that into 0111. Adding 1 to 0111 carries all the way through the 1s and stops at the 0, giving 1000 again. So the bits up to and including the first 1 come out unchanged, and the carry never reaches the bits to their left, which stay inverted. That is true for any number, which gives the shortcut:

The shortcut Working from the right, copy every bit up to and including the first 1. Then invert every bit to the left of it.
Method 2 — the shortcut gives the same answer in one pass 2. invert the rest 1. copy up to and including the first 1 +104 −104 0 1 flip 1 0 flip 1 0 flip 0 1 flip 1 1 same 0 0 same 0 0 same 0 0 same ← start here = −104 Check: −128 + 16 + 8 = −104 ✓
Figure 3 — Same answer as Figure 2, with no addition and no carries to keep track of.
It works both ways

Apply it to a negative number to get the positive one back. 1110 1110 → copy 10, invert the rest → 0001 0010 = 18, so the original was −18.

Edge case: zero

0000 0000 has no 1 to stop at, so every bit is copied and the result is still 0. There is no −0, as expected.

Edge case: −128

1000 0000 → copy 1000 0000 (the first 1 is the MSB) → unchanged. +128 does not exist in 8 bits, so −128 has no positive partner.

Use the shortcut, then check with place values The shortcut is quick but easy to misapply under pressure. Add the place values of your answer using −128 for the MSB. It takes ten seconds and catches nearly every slip.

5. Binary subtraction using two’s complement

A processor does not need a separate circuit for subtraction. Taking away a number is the same as adding its negative:

Method A − B = A + (−B). Find the two’s complement of B, add it to A using ordinary binary addition, and discard any carry out of the MSB.
Example 1: 45 − 18 = 45 + (−18) 1 11 1 1 carries 0010 1101 +45 + 1110 1110 −18 ---------- 1 0001 1011 carry out of the MSB 0001 1011 discard the carry: 0001 1011 = +27 ✓

The carry out of bit 7 is discarded. It is not an error. With two’s complement a carry out of the MSB is a normal part of adding a negative number.

Example 2: 20 − 50 = 20 + (−50) 11 1 carries 0001 0100 +20 + 1100 1110 −50 ---------- 1110 0010 no carry out; MSB is 1, so the answer is negative

The answer 1110 0010 starts with 1, so it is negative. To read it, use the shortcut: copy 10, invert 111000 to 000111, giving 0001 1110 = 30. The answer is −30 ✓.

Example 3: −20 − 30 = (−20) + (−30) 1 11 carries 1110 1100 −20 + 1110 0010 −30 ---------- 1 1100 1110 carry out of the MSB 1100 1110 discard the carry: 1100 1110 = −50 ✓

Overflow in two’s complement

The result of a subtraction must still fit in the range −128 to +127. If it does not, the answer is wrong, and a carry out tells you nothing about it. Example 1 and Example 3 both had a carry out and both answers were correct.

−100 − 50 = (−100) + (−50) 1 11 1 carries 1001 1100 −100 + 1100 1110 −50 ---------- 1 0110 1010 carry out of the MSB 0110 1010 discard the carry: 0110 1010 = +106 ✗ WRONG
How to spot overflow Adding two numbers of the same sign can never legitimately give a result of the opposite sign. Here two negatives (MSB 1) produced a positive (MSB 0). The real answer, −150, is below −128 and cannot be stored in 8 bits. Adding a positive and a negative number can never overflow.

6. One’s complement vs two’s complement

One’s complementTwo’s complement
To negateInvert every bitInvert every bit, then add 1 (or use the shortcut)
MSB place value−127−128
8-bit range−127 to +127−128 to +127
Representations of zeroTwo (+0 and −0)One
Carry out of the MSBMust be added back on (end-around carry)Discarded
Used in modern computers?RarelyAlmost universally for signed integers
Exam scope At IGCSE you must be able to convert positive and negative denary integers to and from 8-bit two’s complement. Binary subtraction using two’s complement is examined directly at AS Level (9618 topic 1.1). It is included here because it shows why two’s complement is designed the way it is.

7. Check yourself

Convert −45 to 8-bit two’s complement.

45 = 0010 1101. Using the shortcut: the first 1 from the right is the LSB, so copy just that 1 and invert the other seven bits: 1101 0011. Check: −128 + 64 + 16 + 2 + 1 = −45 ✓.

1011 0100 is an 8-bit two’s complement integer. Convert it to denary.

The MSB is 1, so it is negative. Place values: −128 + 32 + 16 + 4 = −76. Check with the shortcut: copy 100, invert 10110 to 01001, giving 0100 1100 = 76, so the original was −76 ✓.

Use the shortcut to find the two’s complement of 0101 0000.

Copy up to and including the first 1 from the right: 10000. Invert the remaining 010 to 101. Result: 1011 0000. Check: −128 + 32 + 16 = −80, and 0101 0000 is 80 ✓.

Explain why one’s complement has two representations of zero, and why this is a disadvantage. [2]

Negating a number in one’s complement inverts every bit, so +0 (0000 0000) becomes 1111 1111, a second pattern meaning −0. This wastes one of the 256 patterns, so the range is only −127 to +127. It also means a test for zero has to check for two different patterns.

Calculate 37 − 52 using 8-bit two’s complement. Show your working.

37 = 0010 0101. 52 = 0011 0100, so −52 = 1100 1100 (copy 100, invert the rest). Add: 0010 0101 + 1100 1100 = 1111 0001, with no carry out. The MSB is 1, so the answer is negative. Copy 1 and invert the rest to get 0000 1111 = 15. The answer is −15 ✓.

State the largest and smallest values in 8-bit two’s complement, and explain why the range is not symmetrical.

Largest +127 (0111 1111), smallest −128 (1000 0000). Half of the 256 patterns have an MSB of 0, and they cover 0 to +127. Zero uses one of them, leaving 127 positive numbers. The other 128 patterns all have an MSB of 1 and are all negative, covering −1 to −128.

Calculate 90 − (−50) in 8-bit two’s complement and comment on the result. [3]

Subtracting −50 is the same as adding +50: 0101 1010 + 0011 0010 = 1000 1100. The MSB is 1, so the stored result reads as −128 + 8 + 4 = −116. The correct answer is 140, which is greater than +127 and cannot be represented in 8 bits. Two positive numbers have given a negative result, so an overflow has occurred.