CS01-09 Computer Science Watch
Negative numbers and two's complement
In this lesson
Two's complement: how to explain how a computer represents unsigned and two's complement signed integers. Edexcel 1CP2 GCSE. Binary and number bases 9 of 9. Full Binary and number bases playlist: https://www.youtube.com/playlist?list=PLXHIB7m15mQ8 More at https://scholafly.com
By the end: Explain how a computer represents unsigned and two's complement signed integers, and convert between denary and 8-bit two's complement across the range -128 to +127.
What it covers
- 0:00 Intro: two's complement
- 0:39 Two's complement
- 3:58 Positive numbers
- 8:35 Reading a pattern back
- 12:13 Which is its true value: , , or either?
- 13:43 Back to the freezer
- 15:37 Quick questions
- 17:12 Back to the freezer
Key words
About this video
Two's Complement: negative numbers in binary | GCSE Computer Science | CS01-09
Two's complement: how to explain how a computer represents unsigned and two's complement signed integers.
Edexcel 1CP2 GCSE. Binary and number bases 9 of 9.
Full Binary and number bases playlist: https://www.youtube.com/playlist?list=PLXHIB7m15mQ8
More at https://scholafly.com
Video code: CS01-09 (search "ScholaFly CS01-09")
#GCSEComputerScience #BinaryAndNumberBases #TwoSComplement
For teachers
This GCSE Computer Science lesson teaches negative numbers and two's complement. By the end, students should be able to explain how a computer represents unsigned and two's complement signed integers, and convert between denary and 8-bit two's complement across the range -128 to +127. It works through three worked examples and the mistakes examiners report.
Exam board specification references:
Edexcel 1CP2
- 2.1.2
Read the transcript
A freezer reads minus eighteen degrees Celsius, so where does its chip keep the minus? Imagine that chip stores the temperature as a whole number in one byte: eight bits, each a one or a nought, and none of them a minus sign. The obvious fix is to make the first bit a minus sign and read the other seven as normal. Computers do not store whole numbers that way, and by the end of this you will store minus eighteen the way they do. A word on who needs this. Only one of the exam boards puts it on its GCSE papers, and that board is named right here. If you sit either of the other two, nothing on your papers asks for it, so you can skip this one without a worry.
An integer is a whole number, with no fraction part, and a byte can hold one in two different ways. The first way is unsigned, which means every pattern stands for zero or a positive number. Remember, eight bits give two to the power eight patterns, which is two hundred and fifty-six. That makes the unsigned range, from smallest to biggest, zero to two hundred and fifty-five. The second way is signed, which means some of the patterns stand for negative numbers, the ones below zero. Two's complement is the method computers use to store signed integers in binary, the base-two code of ones and noughts. In two's complement, the most significant bit, the leftmost one, is the sign bit. A nought in the sign bit means zero or positive. A one in the sign bit means negative. The sign bit cuts the two hundred and fifty-six patterns into two halves of one hundred and twenty-eight: one half starts with a nought, and the other starts with a one. Three ranges are in the running. Minus one hundred and twenty-seven to plus one hundred and twenty-seven. Minus one hundred and twenty-eight to plus one hundred and twenty-seven. Or minus one hundred and twenty-seven to plus one hundred and twenty-eight. Which of those is the eight-bit range? Take your pick, and hold on to your reason. It is minus one hundred and twenty-eight to plus one hundred and twenty-seven, the lopsided one. Zero is a pattern too, eight noughts, and it sits in the half that starts with a nought. That leaves room in that half for only one hundred and twenty-seven positive numbers. The half that starts with a one is all negatives, so it gets the full one hundred and twenty-eight, from minus one down to minus one hundred and twenty-eight. The byte still holds two hundred and fifty-six different numbers. They have slid down the number line, so the biggest positive falls from two hundred and fifty-five to one hundred and twenty-seven. The minus sign idea from the freezer would waste a pattern: a minus sign in front of seven noughts is minus zero, a second copy of zero.
A positive number goes into two's complement the same way it goes into unsigned binary, as long as it is no bigger than one hundred and twenty-seven. What is plus twenty as an eight-bit two's complement number? Twenty is sixteen plus four, so it is nought nought nought one, nought one nought nought, with a nought in the sign bit. If turning a denary number, which is our everyday base-ten kind, into eight bits feels shaky, the denary to binary and back video covers it. A negative number takes three steps, in this order: write the positive, flip every bit, then add one. For minus twenty, the first step is done already: the positive is nought nought nought one, nought one nought nought. Then flip every bit, which an exam question may call inverting: each nought becomes a one, and each one becomes a nought. That gives one one one nought, one nought one one. The last step is to add one to that pattern, working from the right and writing every carry, as in any binary addition. The binary addition video has the rules if you need them. Try it on paper before I show you the answer. In the right-hand column, one plus one is nought, carry one. In the next column, one plus that carry is nought again, carry one. In the third column, nought plus the carry is one, and nothing carries on. The other five bits come down unchanged, so minus twenty is one one one nought, one one nought nought, with a one in the sign bit. The reason this works shows up when you add twenty to its flipped pattern. Every column holds one nought and one one, so the sum is eight ones, which is two hundred and fifty-five. That means the flip alone made two hundred and fifty-five take away twenty. Adding one made it two hundred and fifty-six take away twenty. Add twenty to that and the total is two hundred and fifty-six, which in binary is a one followed by eight noughts. A byte keeps only the eight right-hand bits of that total. What number is left in it? Zero. The ninth bit, the leading one, falls off the end and is lost, which is called overflow, and the overflow video covers it. What stays in the byte is eight noughts. So the pattern we built is the number that adds to twenty to make zero, and that is what minus twenty means. That is also why the order is fixed: flip every bit first, then add one. A student flips twenty and stops there. Why is their pattern not minus twenty? Their pattern is only two hundred and fifty-five take away twenty. Add twenty back and you get eight ones, not zero, so it is one short. Adding the one before the flip goes wrong as well, because then you are flipping twenty-one. That lands on one one one nought, one nought one nought, which is minus twenty-two.
Reading a pattern back starts with the sign bit. Take one one one nought, one one nought nought: its sign bit is a one, so the number is negative. One tempting reading treats that leftmost one as a minus sign, then reads the other seven bits as ordinary binary. Those seven bits have ones under sixty-four, thirty-two, eight and four, which add to one hundred and eight. That reading says minus one hundred and eight. The minus sign reading gives minus one hundred and eight. Why is that wrong? This is the very pattern built from minus twenty a moment ago, so its value cannot be minus one hundred and eight. The leftmost bit is not a minus sign stuck on the front. In two's complement it is a column worth minus one hundred and twenty-eight, and every other column keeps its usual value. Turning plus one hundred and twenty-eight into minus one hundred and twenty-eight takes two hundred and fifty-six off the total. That undoes the two hundred and fifty-six that the flip and add one built in. Against that header, the pattern has ones under minus one hundred and twenty-eight, sixty-four, thirty-two, eight and four. Add the positive columns first: sixty-four plus thirty-two is ninety-six, plus eight is one hundred and four, plus four is one hundred and eight. Then the sign column: minus one hundred and twenty-eight plus one hundred and eight is minus twenty. The one hundred and eight was right all along; the slip was a minus sign where two's complement has minus one hundred and twenty-eight. A different question now: flip that pattern and add one. What positive number comes out? Flipped, it is nought nought nought one, nought nought one one, and adding one gives nought nought nought one, nought one nought nought, which is twenty. Getting twenty back confirms the pattern was minus twenty. Now a different one. What number is one one one one, one one one one in two's complement? It is minus one. That is minus one hundred and twenty-eight, plus the seven columns from sixty-four down to one, which make one hundred and twenty-seven. As unsigned, the same eight ones would be two hundred and fifty-five, the biggest a byte can hold, yet as signed they are the negative closest to zero.
Read one one one nought, one one nought nought as unsigned, and the leftmost column is worth plus one hundred and twenty-eight. That makes it one hundred and twenty-eight plus one hundred and eight, which is two hundred and thirty-six. Which is its true value: two hundred and thirty-six, minus twenty, or either? Either one, depending on the program, because the bits do not know what they are. The program that stores them decides whether they mean a signed number or an unsigned one. That is why an arithmetic shift right copies the sign bit into the gap, rather than a nought, and the binary shifts video shows the shift itself. The bits are signed, so minus twenty must halve to minus ten, not jump to a positive. Split into two groups of four, the same bits read E C in hexadecimal, base sixteen. That is three readings of one byte: two hundred and thirty-six, E C and minus twenty.
The freezer's minus eighteen degrees Celsius goes into its byte by the same three steps. Three students each wrote minus eighteen as an eight-bit two's complement number, and only one of them is right. Answer A is one nought nought one, nought nought one nought. Answer B is one one one nought, one one one nought. Answer C is one one one nought, one one nought one. Pick one, then hold on to it before I reveal it. Eighteen is sixteen plus two, so write the positive: nought nought nought one, nought nought one nought. Flip every bit, and it becomes one one one nought, one one nought one. Add one: one plus one is nought, carry one, then nought plus the carry is one. That gives one one one nought, one one one nought, which is option B. Option A only switched on the sign bit, the minus sign idea, and read as two's complement it is minus one hundred and twenty-eight plus eighteen, which is minus one hundred and ten. Option C flipped every bit and stopped. Read back, it is minus one hundred and twenty-eight plus one hundred and nine, which is minus nineteen, one away from the answer.
To finish, a few quick questions, and each one is yours to answer first.
Which bit tells you a two's complement number is negative, and what does it hold?
The most significant bit, the leftmost one, and a one there means negative.
Another one now. What are the three steps for writing a negative number, in order?
Write the positive, flip every bit, then add one.
Switching direction now. When you read a pattern back, what is the leftmost column worth?
Minus one hundred and twenty-eight, while every other column keeps its usual value.
A last one now, as a stretch. With only four bits, what range can two's complement hold?
Four bits give sixteen patterns, and the leftmost column is worth minus eight. That makes the range minus eight to plus seven.
And the freezer's chip keeps minus eighteen degrees Celsius as one one one nought, one one one nought, with no minus sign anywhere in it. Read unsigned, those same bits say two hundred and thirty-eight. The bits still do not know what they are, and now you can read them every way this chapter has taught.
If you can now take a negative number into a byte and back again, give this a thumbs-up, and there is no need to see this twice. If the carries still feel slow, a handful more questions will speed them up. That brings binary and number bases to a close, so look back along the chapter: any video without your thumbs-up is the one to come back to.
The next chapter is Units, characters and file size. Hopefully that's next in your feed, but if not, search for ScholaFly C S oh two, oh oh.
Every other video is on scholafly.com too, easy to search by video code or by keyword. Whatever you're revising, go there and have a look, or, watch the next video.
Related terms
For: Edexcel GCSE 1CP2
On the specification
| Board | Spec | Statement |
|---|---|---|
| Edexcel GCSE 1CP2 | 2.1.2 | Explain how computers represent and manipulate unsigned integers and two's complement signed integers. |
For teachers
This GCSE Computer Science lesson teaches negative numbers and two's complement. By the end, students should be able to explain how a computer represents unsigned and two's complement signed integers, and convert between denary and 8-bit two's complement across the range -128 to +127. It works through three worked examples and the mistakes examiners report.