 In this video, we're going to explain how computer systems encode integers, which of course, as we saw before, they have to do that in binary logic, zeros and ones. So in order to encode integers, computers use two approaches. The first one is fairly intuitive. Suppose we have to encode the number minus 56. We can approach this problem from a very trivial standpoint, which is this is a sign which is either positive or negative. So it's only two possible values, which are very easy to encode. We could use one single bit and encode the plus with a zero and the minus with a one. Once we have decided this encoding of the sign, then we just need to encode this other number. But this other number, as we know, we can encode it as a natural number. In other words, using regular base two. And if you do the operations, this corresponds with the encoding 111000. Now suppose we want to use this scheme and encode our integers using, and this is very important, 8 bits. If we use 8 bits, we know we're going to use one for the sign. So in this case would be minus one. And then we need to expand this code such that it fills the remaining seven bits. In this case would be 0111000. We have to pad this result with an additional zero. So this would be our encoding of integers following this scheme. As you can see these seven bits here are encoding the magnitude. And this single bit here is encoding the sign. And this is the reason why this scheme is called very intuitively sign and magnitude. If we look at the type of numbers we encode here and we draw this representation of the integers, zero here. Suppose these are all the integers. We obviously are going to represent not all of them because they are infinite, but only a subset. So number zero would be here in the middle. The maximum number we can represent with sign and magnitude is actually a zero followed by several ones. As many as we decide the size of our encoding. In this example would be 8 bits, so it would be zero followed by 7 bits. On the other hand the minimum number, which would be this other extreme, the minimum integer we can represent with this scheme would be a one, one, one, all ones. If we decide to use 8 bits would be 8 ones. Now this encoding is very intuitive and the name kind of summarizes the way it works. But it has a little bit of a strange property which is what happens with the representation of the zero. And as we can see the zero right here can have two possible representations. Suppose we stick to our 8 bit size. So this would be one possible representation of zeros, but also this would be another equivalent one. Therefore we end up with two codes for the zero. And this is not very desirable. The other thing that we haven't explained is that once you have decided on an encoding you have to know the rules to perform additions, to perform subtractions, multiplications, divisions, etc. In other words it's not enough to encode the integers, but you also have to figure out how to operate with these integers. Okay so this is the first encoding we have seen. Again sine and magnitude. Very intuitive. However it has this undesirable property over here. Now over the years scientists have come together and realized that there are other possibilities for encoding. Let's represent again this line representing all the integers. Here we have the maximum and the minimum. Now one nice property of this encoding is that all positive numbers have the leftmost bit to zero. So we can say that this is the sine bit. This new encoding that we're going to propose now what it does is the following. It considers a unique representation for the zero, which is all zeros. And what it does is all the negative numbers are going to be represented by the ones starting with a one in the most significant bit. But they're going to be flipped. They're going to be reversed. So this is the number minus one. Minus one is going to be represented in this encoding as all ones. And then we're going to keep decreasing this binary encoding until we reach to the minimum number. And the minimum number is going to be represented by a one, of course, because it's negative, followed by all zeros. So let's take a second to review this new encoding. Rather than having the numbers overlap with two codes over zero, now we only have one single representation for zero. And the only other counter-intuitive property is that the number left to the zero, which is minus one, now has all ones. Another way of looking at it, the negative numbers now are encoded by these increasing values from one followed by all zeros all the way to one one. But it's starting from the minimum to minus one. Now this is, like I said, a little bit current intuitive. So the question is how do we encode a number with this scheme? In other words, if I have an integer, how do I translate it to this new encoding, which by the way is called two's complement? How do I make this transition or this translation? As a matter of fact, there are a few rules that are very easy to apply. So let's work with an example. Suppose I need to translate the number minus 56 from integer to two's complement. The first rule that we have to apply is if the number is positive, the encoding is the regular binary encoding that we apply for natural numbers. So we only need to worry about specific rules for negative numbers. If we have to encode or we have to translate an integer, which is positive to two's complement, we just take its natural representation and that's it. Now for the negative numbers on the other hand, we have to apply three steps. The first one is to convert the magnitude to binary. In other words, 56, forget about the sign for the time being, we have to convert it to binary. And we saw here in the example that 56 is one one one zero zero zero. Now we're going to assume that our integers are representing with eight bits and therefore our correct result needs to be padded with two additional bits here. Zero zero one one one zero zero zero. So this would be a step number one to convert minus 56 to two's complement representation. Step number two, careful with this one, is to flip all the bits. So the representation, instead of being zero zero one one one three zero's, we go to one one zero zero zero one one one. And step number three, perhaps the most important, we have to add one to the result. In other words, we have this number, we do plus one and we perform this addition, which is one plus zero. Sorry, one plus one zero one plus one zero one plus one zero zero plus one one and then zero zero one one. And these we obtained here, the representation of minus 56 in two's complement. So again, repeating, whenever we have a negative number, only negative numbers because positive numbers are represented as regular naturals. When we have a negative number, we convert the magnitude to binary first, flip all the bits, add one. Now, this encoding has a very important and counter-intuitive property. Suppose that I have to encode now number 12. Now as I said before, coding number 12 in two's complement is trivial because the only rule that applies is I encode it as a natural. And again, since we're using eight bits, my representation is going to be zero zero zero zero one one zero zero. So this is represented number 12. Now here comes the most important property of two's complement. What happens if I want to perform this operation? Minus 56 plus 12. So this is basically operating regularly to integers. We can do the operation right away and we know that the result is minus 44. But now look at what happens here. What happens if I take these two binary codes which you'll agree with me are the two's complement encoding of 56 and 12 and I directly apply the regular arithmetic rules of addition. So I add the two numbers. If I add the two numbers, I obtain the combination zero zero one zero one zero one one. Again, this is the result of adding the two binary representations of number minus 56 and 12 represented as two's complement. Now let me use this combination to apply the reverse translation from two's complement to integer. And again, we have to apply three steps. So let's take this example and apply three steps. Step number one to obtain the integer from a representation in two's complement. Which again, it requires only special steps if the number is negative, if it has a one here. If our result had a zero here, then the translation would be trivial because it's just two binary two numbers which you directly translate to the corresponding positive number. But since it is negative, as in the case of this translation from integer to two's complement, we need special steps. And the steps again are three. First one, you flip all the bits again. So this number becomes zero zero one zero one zero one one. Step number two, you add one. So we put a one here. Perform this addition. One and one is zero one and one is zero one one zero one zero zero. And now step number three is translate to natural. We need to translate these numbers to natural. But as we know, this is the bit with weight two to the zero. So this is one two four. So it'll be four plus eight 16 is zero. We don't add that one 32 zero and zero. Therefore, this number is 32 plus eight plus four 44. But we started with a negative number. We know this. Therefore, my result is number 44. Now here is the most important property of two's complement, which is if I take the representation of two's complement of two integers and I apply regular addition rules for arithmetic, regular arithmetic addition rules, the number that I obtain corresponds precisely with the two's complement representation of the result as we have proved over here. The only thing to take into account is that when the numbers are negative, the translation of integer to two's complement and two's complement to integer, they both require these three steps, which I suggest you practice so that you can remember them by heart. Now to conclude, this is actually the preferred representation of integers. And it's mostly for three reasons. The first one, most important one. Addition rule. Addition rule refers to the fact that I can use regular addition in the two's complement code and it works perfectly for integers encoded in two's complement. Second rule, I have one single code for zero. This is very important. The code is more efficient because I do not have this duplication that I used to have here in sine and magnitude. And finally, another interesting property is I still preserve the sine bit. In other words, every integer encoded in two's complement still preserves the property that the leftmost bit or the most significant bit represents the sine.