-[Craig] In this video, we take a look at signed integers using two's complement.
(uplifting piano jingle)
So we know from our previous video that the smallest,
or you could say lowest number, we can represent in binary is zero.
And if we were using eight bits, it would simply be a sequence of zeros in every column.
We also know that if we add ones at all in any column, the number becomes positive and increases in value.
So even if we put a one in the smallest column weighting on the right,
we still have a value that's increased from zero.
So this begs the question, if adding any ones at all increases the value of a number,
how can we possibly represent negative numbers in binary?
For example, minus 10.
So let's just step back and think about a little analogy.
Imagine a car's milometer.
So the car leaves the factory and all the digits are set to zero,
and we can think of this as being the value zero.
Drive the car for one mile and the milometer ends up at 000001 in this situation.
And we could think of this as representing positive one mile.
Imagine we could turn the meter back one mile, however, from the starting position,
it would now read 999999.
And we could think of this or interpret this as minus one mile.
Computers are able to use a very similar concept called two's complement.
We're going to take a look at that now.
So before we dive into how it actually works, a couple of observations.
Here are the numbers minus three up to positive three in two's complement.
You will notice that when using the two's complement method of representing binary numbers,
that all positive numbers always start with a zero.
And in mathematics, zero is considered a positive number, so this works fine.
Likewise, you'll notice that all negative numbers always start with a one.
So let's take a look at how this actually works.
So here's our standard binary weighting line.
Starting with a one on the right-hand side and then doubling as we move left,
2, 4, 8, 16, et cetera.
You'll notice that when using the two's complement method,
the most significant bit, the MSB, so that's the left-most bit,
now represents a negative value.
So we've gone 16, 32, 64,
and instead of the left column representing 128,
it now represents minus 128.
Okay, so now we've changed our weighting line, let's try representing the positive number 117.
Well it's a positive number,
therefore the most significant bit must be a zero, so we'll pop that in.
And then obviously, like we've shown you in the previous video,
we pop ones in every column we need to add the values to 117.
So we have a 1 in the 64 column plus a 32,
plus a 16, plus a 4, plus a 1, and we have a positive 117.
Well now let's store the negative version of 117.
Well as mentioned earlier,
all negative numbers in binary start with a one in the left-hand column if we're using two's complement.
Because we have a value of a minus 128, because we have a one in that left column,
we've got one lot of a minus 128,
we now need to place ones in the columns to bring the value up to minus 117.
So if we put a one in the eight column,
we're saying minus 128 plus positive 8,
so that's bringing us up to minus 120.
We then put a one in the two column.
And now this brings our value up from minus 120 to minus 118.
And finally, we put a one in the one column,
and that brings us up from minus 118 to the target value we want of minus 117.
Now there is actually a really neat trick that you can use
that always works, which turns a two's complement number into its negative version.
So the process is as follows.
Start by writing out the positive version of the number.
Then starting from the most right-hand digit, so that's the least significant bit,
you copy out each digit exactly as they appear, up to and including the first one you come across.
After this point, you continue, but now you swap every one for a zero and every zero for a one.
So let's actually look at this in practice.
We're going to convert the number positive 12 into negative 12.
So first we write out the positive version of the number,
so here's the number 12 written in binary.
We've got a one in the eight column plus a one in the four column,
8 plus 4 is 12.
We then start from the right-hand side, the least significant bit.
And we simply copy every value working left,
up to and including the first one, so you can see we've done that there.
Zero becomes zero, zero becomes zero,
one becomes one.
After this point, we now switch.
So where we had a one, we instead write a zero, and where we had a zero, we then write a one.
What you've ended up with is the negative version of 12, and this works every time.
Now in a video coming up in this series, we're going to show you how to add two binary numbers together.
Once you've watched that video, you'll be able to prove to yourself
that what you've got on the screen here is correct.
Because as you know, if you add plus 12 to minus 12, you should end up with zero.
And indeed if you perform addition on these two numbers,
you will end up with a row of eight zeros.
Give that a go once you've watched the video.
(uplifting piano jingle)