LogicGates.org Open the simulatorSimulator
Roadmap

Negative numbers and subtraction

Lesson 4 of 6 in this stage, about 15 minutes

Two's complement: make the top bit negative and the same adder subtracts.

There is no minus sign on a wire

A four bit circuit holds sixteen patterns and nothing else. If you want negative numbers, some of those patterns have to mean negative numbers, and the circuit has to be told the rule. The rule almost every computer uses is called two's complement, and it changes one thing about place value: the top bit is worth minus its usual weight. In four bits the weights are 8, 4, 2, 1 for unsigned numbers; in two's complement they are −8, 4, 2, 1. Every other bit is as before. So 1011 is −8 + 2 + 1 = −5, and 0101 is plain 5, because its top bit is 0 and contributes nothing.

The same bits, two readings. Click a bit, or negate the number.

0101 is 5 read as unsigned, and 5 read as two's complement.

Patterns with a 0 on top are the non-negative numbers, exactly as in unsigned binary. Patterns with a 1 on top are the negatives, counting up from the most negative. Here is the whole four bit table, read both ways:

Bits Unsigned Two's complement
0000 0 0
0001 1 1
0010 2 2
0011 3 3
0100 4 4
0101 5 5
0110 6 6
0111 7 7
1000 8 −8
1001 9 −7
1010 10 −6
1011 11 −5
1100 12 −4
1101 13 −3
1110 14 −2
1111 15 −1

The range

With n bits the most negative number is a 1 followed by zeros, worth −2n−1, and the most positive is a 0 followed by ones, worth 2n−1 − 1. Four bits run from −8 to 7, eight bits from −128 to 127. There is one more negative number than positive because zero takes one of the patterns with a 0 on top. That is also why −8 is odd one out: it has no positive partner in four bits.

Negating: invert and add 1

To find the pattern for a negative number, write the positive number in binary at the full width, flip every bit, and add 1. The same two steps go the other way too: they turn a negative pattern back into its positive partner. Press Negate in the widget above to watch them.

Worked example. What is −5 in four bit two's complement?

5 is 0101. Invert every bit: 1010. Add 1: 1011. Check it by reading the weights −8, 4, 2, 1: −8 + 2 + 1 = −5. Going back the same way, invert 1011 to get 0100 and add 1 to get 0101, which is 5 again.

Common mistake: reading the top bit as a plain minus sign

1011 is not −3. Writing a minus sign and then the size, the way we do on paper, is a different code called sign-magnitude, and adders do not work with it. In two's complement the top bit is a weight, −8, that you add to the rest: −8 + 3 = −5.

Subtraction is addition

Here is the reason this code won. To work out 6 − 3, add 6 and −3. In four bits that is 0110 + 1101, and the ripple carry adder from the last lesson gives 0011 with a carry out of 1. Throw the carry away and 0011 is 3. Right answer, ordinary adder, no new circuit. Try it below: a is 6 and b is the pattern for −3.

6 + (−3). The b row holds 1101, the pattern for −3. Ignore the carry out.

Row out 8421
carries 11000
a
+ b
= sum 1 0011

Read as two's complement, 6 + (−3) = 3. The sum bits 0011 read as 3, and the carry out of 1 is ignored.

In hardware, the "invert and add 1" happens on the way in. A row of NOT gates (or XOR gates with a control line) flips every bit of b, and the adder's first carry in is set to 1 to do the "add 1". One control signal switches the circuit between a + b and a − b, which is why a processor has an adder and no separate subtractor.

Why?: why does adding the pattern for −3 subtract 3?

Read as unsigned, the pattern 1101 is 13, and 13 = 16 − 3. So 6 + 13 = 19 = 16 + 3. In a four bit circuit the 16 falls off the end as the carry out, and what is left is 3. Adding 2n changes nothing in n bits, so "add 16 − 3" and "subtract 3" come out the same. Two's complement is built on exactly that fact.

Overflow for signed numbers

The carry out no longer means what it did. 6 + (−3) had a carry out of 1 and was perfectly correct. For signed numbers, overflow is when the true answer is outside the range, and it shows up as the wrong sign. 5 + 3 is 0101 + 0011 = 1000, which reads as −8: two positive numbers gave a negative, so 8 did not fit in −8 to 7. The same happens when two negatives give a positive. Adding a positive and a negative can never overflow, because the answer is always between the two.

What to remember

  • Two's complement makes the top bit worth −2n−1; every other bit keeps its weight.
  • Four bits hold −8 to 7; eight bits hold −128 to 127.
  • To negate a number, invert every bit and add 1. The same steps go both ways.
  • Subtraction is adding the negative, so one adder does both jobs.
  • For signed numbers, ignore the carry out; overflow is two same-sign numbers giving the other sign.

Check yourself

Get 5 right in a row and the lesson is done. A wrong answer costs the run, not the lesson.

0 right in a row. 0 / 0 this visit

In four bits, 2 is 0010. What is the pattern for −2?

0010

Already know this? and come back to the quiz any time.

Go deeper