 We've now looked at three different ways of writing down the numbers that we have in binary for our computers. We've seen sign and magnitude, one's complement, and two's complement. So the next thing we'd like to do is ask, well, which one should we use? Does one of these work better than the others? Which one do we actually use in our computers today? To do that, we're going to start by asking, well, what do we want our numbers to do? If we're just interested in using them for communication, then sign and magnitude seems like it would be the easiest. We use one bit for the sign, and the rest is just the magnitude. We're not doing anything terribly funny. It's really easy to tell if you've got a positive number or a negative number. Relatively easy to tell what that number is. Whereas with one's complement or two's complement, you have to perform all sorts of odd operations. If you have a negative number and you'd like to see what the magnitude is. So if the only thing we're interested in was being able to talk about numbers, being able to show them and read them, then we'd probably want to go with sign and magnitude. But we actually do a whole lot more than just read and write numbers. We usually want to do lots and lots of computation with them, far more than we do actually reading and writing them. So instead, we'd like a format that makes our arithmetic easy. Well, how hard is it to do arithmetic with these? Let's find out. I'm going to start with a couple of numbers in sign and magnitude. We saw that that was relatively easy to read and write, so if that does arithmetic well, then great. So I'm going to start with a simple problem, 5 plus 3. Hopefully everything works out well. In this case, I'm just going to use 8 bits to represent each of my numbers since they're all small. That will be sufficient to illustrate the problem for our purposes. So 5 will be 3. Now if I just apply our regular addition algorithm to these numbers, 1 plus 1 will give me 10 and 0 plus 0 gives me 0. So 5 plus 3 does indeed get me 8. Well, let's try some other options. What if I have... I really want to do 3 minus 3, but since I'm just doing addition, we'll use negative 3. Negative 3 is really easy to write in sign and magnitude anyway. I should expect that this should give me 0 as a result. So in binary, these will look like... So my regular addition algorithm worked just fine up here. I'll try that again down here. 1 plus 1 gives me 0, plus 1 plus 1 gives me 1, carry a 1, 1, and 1 plus 0 gives me 1. So I added 3 and negative 3 and I got negative 6 out. That was clearly not what I want. So this doesn't look so good. I'll try one more example. Maybe I just picked a bad example and this one might be better. So now I have 3 and negative 5 to add together. There's my 3. There's my negative 5. Well, this time at least the sign is right. I have negative 8. It's still not what I wanted. It's captured the sign nicely for me, but it's basically done 3 plus 5 and then told me, oh well, it should be negative. So this isn't terribly helpful. I would have to come up with something else to handle the case where I'm adding a positive number and a negative number. This might work okay for two negative numbers. Try. So this should give me negative 6. But it turns out it gives me positive 6. So sign and magnitude clearly doesn't work too well any time you've got negative numbers to add together. So I probably don't want to use sign and magnitude if I'm actually doing arithmetic. How about one complement? Let's try some of those same problems again. I'll start with 5 plus 3. That one worked well for sign and magnitude. If it doesn't work well for one's complement then I have an even bigger problem. 5 plus 3 should give me 8. Fortunately, 5 and 3 both look exactly the same in one's complement as they do in sign and magnitude. And the addition operation works exactly the same as well. So I really should expect to get the same thing back out. So yes, I got 8 out. So one's complement works just as well for positive numbers. Which is reasonable since we're representing our positive numbers exactly the same way. The difference between one's complement and sign and magnitude is how we represent our negative numbers. So let's try that 3 plus 3. This should equal 0 again. So there's our 3 as before. For negative 3, we just take all the bits in our 3 and we'll negate them. So we get... Now we want to add these two together. So I have a whole lot of 1 plus 0. So I get this number. It's a negative number. Now if I invert all the bits in this number I get negative 0. That seems a little odd. We don't normally talk about having a negative 0 but that doesn't seem too bad. It looks like it works right? It still tells us that we have 0. It just attached a sign to it. We could just ignore the sign. So let's try that 3 plus negative 5. This should give me negative 2. For 5, I would take 5 and invert all of the bits. Now I'll add these two together. My leading bit is 1 so I have a negative number. So I will go through and flip all of the bits. And I'll notice that that means that this is negative 2. Which is exactly what I wanted. So great. It works for addition. It works when I'm doing subtraction. It has a minor issue for 0 but that's okay. So let's try our other example here. So negative 3 is always this. And 3 is... So I'll add these two numbers together. 0, 0, carry 1, carry 1, 1, carry 1. And I'll just discard that extra 1. Again, I have a leading 1 so I have a negative number. It's good. I'm hoping to get negative 6 out of this. But this actually gives me negative 7. It seems odd. That's not what I wanted to get. I got two negative numbers together and I got something that's off by 1. So once complement does better than sign of magnitude but we can still end up with some problems when we're adding two negative numbers together. So we can try the same thing with 2's complement. It won't actually be very interesting to do this with the addition because we still represent our positive numbers exactly the same way we've done before. So if it worked for 1's complement it worked for sign of magnitude. It'll work just fine for 2's complement because both of those are positive numbers and we're still representing our positive numbers the same way. We're still doing our addition the same way. So let's try the other three problems. I have a 3 plus negative 3 which should hopefully get me 0. Positive numbers still look exactly the same. For negative numbers I flip all of the bits the same way I did for 1's complement and add one more to it. So I'd have a 0 there, 0 plus 1 gives me 1. So now I have 1 plus 1 gives me 0, 0... Oh, great! I have 0. It doesn't even look like a negative 0. I just have 0. So that gets rid of that minor issue that we saw with 1's complement. 3 plus negative 5 so I'd have a 0 there and 0 plus 1 gives me 1. Now I'll add those two together. 1 plus 1 gives me 0, carry a 1 plus 1 plus 1 is 1, carry a 1. So my leading bit is a 1 so I have a negative number. I will flip all of the bits. It gives me 0, 1 and I'll add one more to this to make this 1, 0. So I do in fact get negative 2 out. So this works at least as well as 1's complement. We've solved the first three problems just as well and even a little better for the case with a negative 0. Let's try the one last one. So we saw this was negative 3. Add these together. So I have this number, the leading bit is a 1 so it's a negative number. I'll flip all of the bits, add 1 to this and give me 1, 1, 0 which is negative 6. So this actually gave me the right answer. It turns out 1's complement has trouble because of the extra 0 that it's got, that it can represent a negative 0 as well as a positive 1. We don't normally have any interest in negative 0 so it doesn't make much sense to support that in our number system. Additionally that also gave us problems of trying to add two negative numbers together so we really don't want to do that. 2's complement gets away from that by adding 1 to the negative numbers. This gets rid of our negative 0 problem because we'd add 1 to this number and we'd get back to 0 again just the way we did here. This also meant that adding two negative numbers together worked out correctly. So while we've used both sign and magnitude and 1's complement formats in actual hardware, modern hardware uses 2's complement. The vast majority of the operations we do are arithmetic rather than just reading numbers so we're much better off having a nice simple system that can easily do arithmetic for us even if the numbers are a little harder to read. So later when we're actually building hardware to perform arithmetic for us, we're going to be working with 2's complement because it makes the arithmetic nice and easy.