Signed 2's complement (or sign 2's complement) (s2c) is a modification of the sign-magnitude form in which addition and subtraction work the way that you expect them to. The price we pay is that we can't read a negative number directly. The high order bit is still the sign bit, and a ``1'' still indicates a negative number. Furthermore, zero is represented by all zeros and, for this course, a ``1'' followed by all zeros is still an illegal value for us.
The relationship between a number and its s2c equivalent is formalized in the following, and explained below.
The s2c form of a positive integer is identical to the sign-magnitude form of the positive integer. So, our ``example'' number 2087 is still given by 13 bits: twelve to capture the magnitude of the number, and one, the msb, set to 0, to signify that the number is positive.
In order to encode or determine the s2c form of a negative integer, we first write down the s2c form of its positive form. Then, we bit-wise complement all bits of that number (the zeros become ones, the ones become zeros). This is called forming the one's complement. Once we've done that, we form the 2's complement by adding 1 to the bit string that we have. The result is the s2c form.
So, for -2087, we start with
To ``decode'' a number in s2c form, first check the sign bit. If the sign bit is zero, the conversion is identical to that shown for sign-magnitude positive numbers above. If the sign bit is one, then we know the number is negative. Now that we know the sign, we must decode to get the magnitude. This is done by complementing all the bits and adding one. The magnitude of the resulting number will be that of the negative number you started with.
For example, consider the following 13-bit number in s2c form:
Since the above value is positive 2 in binary, the value we started with, all ones except for the least significant bit, must be the s2c form of -2.
Note: Suppose you are asked to compute a s2c equivalent of either positive or negative 2087 above, but the representation must be longer than 13 bits, say 16. All you have to do is extend the sign bit! If the number is positive, fill to the new msb with zeros. If the number is negative, fill to the new msb with ones. This sign extension works for any s2c value.
MM Hugue 2017-08-28