Cookies   I display ads to cover the expenses. See the privacy policy for more information. You can keep or reject the ads.

Video thumbnail
You might already be familiar with binary for example. This is
1 0 1 is equivalent to 5 in decimal that's because this is the ones place. This is the twos place
fours place eights place
16s
32s
64s and 128s
and we have a 1 in the fourth place and a 1 in the ones place 4 plus 1 is is 5
So this is fine, but how might we represent a negative number, so let's say we wanted to [represent] negative 5
Well, there's a couple ways we can do that one way is to take this
128s place and instead of using that as the 128s place use that as a sign so change this to a 1 here?
To indicate [that] this is negative, and then the rest of it is the same
1 0 1 and so this would be 5 here and then instead of this representing
128 it represents that the number is negative, so [negative] 5 now of course. It's important to know how many bits
You're working with right because if we're only using 4 bits then a 5 would be 0 1 0 1 that's equal to 5
[but] then we're going to use this this top bit here in this case
We're only using 4 bits to the top that is this fourth bit and so negative 5 might be a 1 1 0 1
And now instead [of] this being the eights place
This is actually representing a sign
So this would be negative 5 and this would be
5 so it's important to know how [many] bits you're working with so [that] you know which bit is the first bit and therefore which
Bit indicates what the sign of the number is so to keep going with this example. We can look at just
regular counting and binary here of course if we have all 0 0 0 0 0 that's equal to 0
And then if we have a 1 in the ones place that's equal to 1 we have a 1 in the twos place
That's equal to a 2 and a 1 that's equal to 3 and then of course a for is for
a 4 and a 1 is 5 a 4 and a 2 is 6
For a 2 plus a 1 is is 7 so that's simple enough, and then if this this first
Bit here is is indicating our negative sign?
then we can go backwards to so if we have a negative 1 that's negative [one] a negative to a
Negative you know [2] and a 1 is 3 and then a negative negative 3 negative
4 negative
You know 4 and a 1 is 5 with a negative is negative 5
4 2 2 is 6 so this is negative 6 and then a 4 plus 2 plus 1 is 7 and?
That's a negative 7 and so this is our sign bit
And so this first bit is our sign bit and then these other bits are just our 1 2 & 4 place
simple enough
Couple weird things about this though one is that you'll notice there's a negative 0 right because you can have 0 0 0 and then
It can either be 0 in the sign bit place or a 1 in the sign bit place
So you can have you know there's a difference between 0 and negative 0 so that's that's kind of weird
[the] other thing that is maybe a little bit inconvenient
We'll look at some some other approaches that that don't have this problem is
If you try to add these things together things get kind of weird
so let's say we want to add a 5 and a negative 5 so normally 5 plus negative 5 you would expect to get 0
simple enough, but here if we look at 5 0 1 0 1 and
negative 5 is 1 1 0 1
If we add these together [1] plus 1 is 2 which
be a 0 and then carry the 1 1 plus 0 plus 0 is 1
1 plus 1 again is to the but will
that is a 0 and carry the 1 and then here 1 plus 1 again is 2 so 0 and carry the 1
So in this case what we're seeing is 5 plus a negative 5 is not 0 it's 0 0 1 0 which
Well, and we have a carry so we have a carry coming out of this this this one
Bit that we don't you know if we're working with 4 bits?
we're going to ignore this this carry bit and so we have 0 0 1 0
which is
which is 2
Well, that's kind of weird
We're adding 5 and negative 5 we wouldn't expect to get [2] and you can try adding some other things here
It doesn't it doesn't work
So let's take a look at another scenario here. This is called one's complement. What this is
Is again everything from zero to seven is the same same same as we saw before and this first bit here is all zeros
So we know that these are all positive
But what we do for the negative numbers is we actually just flip all of the bits
We take the compliment of all of the bits, so you know [2] here is 0 1 0?
Well 0 0 1 0 negative 2 is 1 1 [0] 1 so we're just flipping each of those bits
so we flipped the 0
401 We Flipped the 1 4 0 with the 0 [4] a 1 and, so on if you look at each of these numbers
So 5 is 0 1 0 1 negative 5 is 1 0 1 0?
So what happens with with this so we still have this kind of strange thing where we have negative 0 and we'll come back to
That in a minute
[but] what we can do is we can try to you know add some numbers, so if we add 5 So 0 1
0 1 that's 5 and
negative 5 1 0 [1] 0
We add those
adding negative 5 here
[what] we end up getting is 1 plus 0 is 1?
0 plus 1 is 1 1 plus 0 is 1 0 plus 1 is 1 we get 1 1 1 1 which is negative zero
So that's closer, right?
You know 5 minus 5 you expect to get 0 negative 0 is I guess the same thing. [so] that's pretty good
And and that actually works for any of these you know we can do 3 minus 3 or 3 so 3 is?
0 0 1 [1] negative 3 is 1 1 0
0 so this is 3 plus negative 3 if we add these together [so] [1] plus 0 is [1]
1 plus 0 is 1 0 plus 1 is 1 0 plus 1 is 1 we get 1 1 1 1 again [and]
[so] again 3 minus 3 is negative 0 so this is definitely doing doing a lot better than
then here where we were saying a 5 minus 5 was was 2
That's definitely not right
So negative 0 definitely a lot closer. Let's try some other some other things kind of see if we can see a pattern here
so if we did let's Say 5
Plus negative three so five minus three let's see what we get there
five is 0 1 0 1
and negative 3 is 1 1 0 0
so 1 plus 0 is 1 0 plus 0 is 0
1 plus 1 is [2] so we'll put a 0 and carry a 1 and
then 1 Plus 1 again is 2 so we'll put a 0 and carry a 1
And so if we ignore this this first bit here because we're working with 4 bits we get 1
So 5 minus 3 is [1] that's close. It should be 2 but you know let's let's see. What happens if we do
Say 6 minus 2 or 6 plus negative 2
So 6 is
0 1 1 0
negative 2 is 1 1 0 1 1 1 0 1
[0] plus 1 is 1 1 [plus] 0 is 1 1 plus 1 is 2 0 and carry the 1 1 plus 1 is 2?
put a 0 and carry the 1
again ignore that first [bix] [we're] working with four bits so 0 0 1 1 is 3 so
[we're] saying [sex] minus 2 is 3
close
But but we're definitely we're definitely seeing an interesting Pattern here in fact if we look
You know these places where we're getting this negative 0 if we just go down 1 we see zero
Which is what we want here 5 minus 3 we'd expect to see two, but we're seeing one
so if we just go down 1 we'd actually see a 2
6 minus 2 we'd expect to see 4 but we're actually getting 3 but if we go down 1 we get the right answer
So we could just always add [1]
To each [of] these of course it's kind of weird to say add 1 2 to negative 0 to get to 0 but
If you kind of bear with me a minute
You'll see that if we add 1 to the binary value in it and it flips over
You do get to the right thing and here you get to the right thing
So the ones compliment. You know just flipping all of these bits is
Close it's very close. It's off by 1 on all on all of our arithmetic
And it also still has that nice property that we saw with the you know with [the] sign bit
Which is you know you can look at this first bit and you can tell these are all negative numbers
And if the first bit is [zero], then they're positive numbers
so it seems like the ones complement has a lot of the
You know a lot of nice properties. [you] know that you'd have in the sign, bit the one thing. That's you know well
I guess it's not too bad is
It to get from to inverter to negate a number
You know if you're going from five to negative five with a sign bit all you'd have to do is flip one bit
You just flip that first bit to a one and you get to negative five
Well with the ones complement you have to invert each bit so it's a little bit more work
But of course inverting bits is is pretty easy to do in hardware if we're building hardware to do this for building a computer
I'm going to be doing some math, so
[once] [compliment] seems pretty good, but it is off by by one
So what we can do is look at another scenario
Which is two's complement and if we compare ones complement to two's complement?
It's basically the same except what it does is it gets rid of this negative zero?
[so] instead of having negative zero we just go [right] to negative one
Negative two three four five six seven and the the kind of weird thing here is we have a negative eight
And we don't have an eight, but the nice the nice property about this is that all [of] our math works out very nicely
so if we have [five] minus five
5 here is zero one zero one and
So that's 5 and negative 5 is 1 0 1 1 1 0 1 1
and if we add these things if we add negative 5
1 plus 1 is 2 so we'll put a 0 and carry the 1 1 plus 1 is to
put a 0 and carry the 1 1 plus 1 is 2
so we'll put a 0 and carry the 1
And 1 plus 1 is to put a 0 and we have [a] one that we're carrying and so you'll notice that each
when we're when we're looking at the the
Negated version of something when we add each of these
Place values we get a 2 each time, [and] that's why it's called two's complement
But in any event you see that we end up with 0 which is just what we would want when we add a number
Or to its its inverse and in fact you can try it with any of these numbers
and you'll find out [that] you you get all [zeroes], so
[Let's] try you know 6 minus 2 so 6 is 0 1 1 0
that's 6 and
Minus 2 is 1 1 1 0 1 1 1 0 and?
if we add these together we add our negative 2 oops Negative 2
adding negative 2 there 0 plus 0 is 0
1 plus 1 is
2 so we'll
Put a 0 and carry the 1 1 plus 1 plus 1 is 3 so [that's] a 1 and a carry a 1
And the 1 plus 1 is
2 so 0 and carry a 1 and again we ignore that fifth bit there and so 0 1 0 0 is
4 and sure enough 6 minus 2 should be equal 4 so this two's complement works works pretty well
[and] it also has this nice property again that that first bit is a sign bit right because all of these negative numbers
Have a 1 there
And what's even more interesting about two's complement is if you look at the place values. It actually makes sense
This is our ones place. This is our twos place. This is our forest place and this place here
It's you know. It looks like a sign bit, but really it's a negative 8 place
and
You can see when [we] have a negative 8 and all zeros
We have a negative 8 but if you have a negative 8 and a 1 you get negative 7 right?
if you have negative 8 and a 4 you get negative 4 if you get negative 8
Plus 4 Plus 2 plus 1 you end up at Negative 1
So this this this bit value actually has has a mathematical meaning. Which is why our math works out, so it's pretty cool
The one thing that's a little bit harder with two's complement
Is is negating a number? So if we want to go from a 5 to a negative 5?
it's kind of a two-step process right so 5 is 0 1 0 [1]
The first thing you have to do is take the ones compliment 1 0 1 0 so flip all the bits
So you're just inverting everything that's that's easy enough, but then you have to add 1
So you invert?
and
Then add [1] so 2 add 1 2 0 [1] 0 1 is just 1 0 1 1 and then this
Invert so this is 5 then we invert it and then we add 1 and that gets us [to] Negative 5
you see negative 5 1 0 1 1
[1] 0 1 1 so it's a little bit more work to go from a positive number to [2] its inverse
Because you have to invert it and then add 1
But what you'll see you know in the next video when we when we build a circuit that can add and subtract
You'll see that it's it's actually not too [hard] to build to build this ain't hardware