WEBVTT
00:00:02.889 --> 00:00:08.600
Two's complement representation is a way of
representing both positive and negative numbers
00:00:08.600 --> 00:00:12.469
in binary.
00:00:12.469 --> 00:00:17.730
Let's take a look at an example of a positive
number represented in two's complement in
00:00:17.730 --> 00:00:22.090
order to begin understanding how this representation
works.
00:00:22.090 --> 00:00:28.670
If we look at the number, 001000, the way
that we determine the value of this two's
00:00:28.670 --> 00:00:31.279
complement number is as follows.
00:00:31.279 --> 00:00:37.940
Each bit represents a power of 2, with the
right most bit representing 20,
00:00:37.940 --> 00:00:45.079
the next bit is 21, then 22,
23, 24, and finally
00:00:45.079 --> 00:00:46.949
-25.
00:00:46.949 --> 00:00:51.350
Having the most significant bit represent
a negative power of 2 rather than a positive
00:00:51.350 --> 00:00:57.769
power allows us to express both positive and
negative numbers with our notation.
00:00:57.769 --> 00:01:04.250
If your number has a 0 in a certain position,
then the power of 2 associated with that position
00:01:04.250 --> 00:01:06.640
is not present in your number.
00:01:06.640 --> 00:01:11.780
If the number has a 1, then that tells us
that the corresponding power of 2 makes up
00:01:11.780 --> 00:01:13.960
part of the number's value.
00:01:13.960 --> 00:01:21.539
To figure out the value of the number 001000,
we see that there is a single 1 which is in
00:01:21.539 --> 00:01:22.990
the 23 position.
00:01:22.990 --> 00:01:27.490
So this number is equal to 23 which
equals 8.
00:01:27.490 --> 00:01:33.030
Note that if I wanted to represent the same
number 8 using a larger number of binary bits,
00:01:33.030 --> 00:01:36.670
I would simply add some leading zeroes to
my positive number.
00:01:36.670 --> 00:01:42.950
For example, the number 8 represented in 8-bits
two's complement is the same as the 6-bit
00:01:42.950 --> 00:01:49.460
representation but with 2 additional leading
zeroes.
00:01:49.460 --> 00:01:52.329
Now let's try a negative number.
00:01:52.329 --> 00:01:57.200
Negative numbers in 2's complement representation
always have a 1 in their most significant
00:01:57.200 --> 00:01:58.509
bit (MSB).
00:01:58.509 --> 00:02:00.880
What is the value of 101100?
00:02:00.880 --> 00:02:10.000
We apply the same logic as before, now we
have a 1 in bit positions 2, 3, and 5.
00:02:10.000 --> 00:02:17.950
That means that this number is equal to -25
+ 23 + 22 which equals
00:02:17.950 --> 00:02:24.150
-32 + 8 + 4 = -20.
00:02:24.150 --> 00:02:30.610
In the case of negative numbers, if we wanted
to represent the same value, -20 using more
00:02:30.610 --> 00:02:35.879
bits, one would simply need to append leading
1's to the number to end up with the same
00:02:35.879 --> 00:02:37.340
value.
00:02:37.340 --> 00:02:45.980
For example, -20, using 8 bits is 11101100.
00:02:45.980 --> 00:02:48.250
Let's check that this is correct.
00:02:48.250 --> 00:02:56.090
This number is equal to -27 + 26
+ 25 + 23 + 22
00:02:56.090 --> 00:03:18.290
which is equal to -128 + 64 + 32 + 8 + 4 which
is -128 + 108 and that is equal to -20.
00:03:18.290 --> 00:03:24.130
Let's take a look at the range of numbers
that can be represented using N-bit 2's complement.
00:03:24.130 --> 00:03:26.850
Let's begin with our example of 6-bits.
00:03:26.850 --> 00:03:35.269
The largest positive number in that case is
0 followed by five 1's (011111) which is equal
00:03:35.269 --> 00:03:41.190
to 20 + 21 + 22
+ 23 + 24 which is equal
00:03:41.190 --> 00:03:52.010
to 1 + 2 + 4 + 8 + 16 which equals 31 which
is also equal to 25-1.
00:03:52.010 --> 00:03:59.099
The most negative number using 6 bits is 1
followed by five 0's (100000) which equals
00:03:59.099 --> 00:04:07.110
-25 with no positive component
added to the number and that is equal to -32.
00:04:07.110 --> 00:04:16.660
So in general for N-bits, the 2's complement
number can range from -2N-1 through
00:04:16.660 --> 00:04:26.670
2N-1-1.
00:04:26.670 --> 00:04:31.560
An easy way of converting between positive
and negative 2's complement numbers is by
00:04:31.560 --> 00:04:34.310
flipping all the bits and adding 1.
00:04:34.310 --> 00:04:41.610
For example, earlier we calculated that 101100
= -20.
00:04:41.610 --> 00:04:47.620
If I take this number and flip all of the
bits, I get 010011.
00:04:47.620 --> 00:04:52.980
I then add 1 to that, which results in 010100.
00:04:52.980 --> 00:05:01.580
This number is = 22 + 24
= 4 + 16 = 20.
00:05:01.580 --> 00:05:09.070
This tells us that our original number 101100
was equal to -20.
00:05:09.070 --> 00:05:12.350
The same methodology can be applied in reverse.
00:05:12.350 --> 00:05:21.410
If you start with a positive number 20 = 010100,
and you flip all the bits and add 1, you get
00:05:21.410 --> 00:05:27.040
101011 + 1 = 101100.
00:05:27.040 --> 00:05:31.070
This brings us back to our original number
which is -20.
00:05:31.070 --> 00:05:36.870
So to easily determine the value of a negative
2's complement number, one flips all the bits
00:05:36.870 --> 00:05:42.810
and adds 1 to find the corresponding positive
number, say X, which in turn means that your
00:05:42.810 --> 00:05:46.430
original negative number had a value of -X.