Remember the following mantra:
Flip all the bits and add 1. |
Like 1's complement, the number of bits must be fixed and you must pad out the number if it is too short. If the number is too long for your fixed size, then you can't represent it using 2's complement of that bit-width.
To illustrate, let's work through converting -19 to 2's complement, using a fixed size of 8 bits.
Step 1: Get the value in binary. 19 is 10011. Step 2: Pad out to 8 bits. 00010011 Step 3: Flip all the bits. 11101100 Step 4: Add 1. 11101100 +1 -------- This is it! 11101101
Here's another, a little more interesting one: -32 (again using 8 bits).
Value of +32 in binary 100000 Pad out to 8 bits 00100000 Flip the bits 11011111 Add 1 +1 -------- The answer 11100000
Let's see what happens when we convert -0 to 2's complement.
Value of 0 in binary 0 Pad out to 8 bits 00000000 Flip the bits 11111111 Add 1 +1 -------- The answer 00000000
In this case, the carry propagation never stops, but travels way behind the leftmost place. Since we can only store 8 bits, we throw away this 9th bit, and get 00000000. Thus, 0 has but one unique representation in 2's complement, and there is no separate -0 as there is in 1's complement.
Remember that a positive integer does not need to be flipped, although it must be padded with 0s on the left end to achieve the set number of bits. Thus, +19 in 8-bit 2's complement is 00010011. A common mistake that student make is to flip and add 1, even if the number is positive. This is definitely wrong!
Following are the values given above in 2's complement, using various bit sizes.
number 4 bits 8 bits 12 bits ------ ------ ------ ------- 0 0000 00000000 000000000000 1 0001 00000001 000000000001 -1 1111 11111111 111111111111 -15 can't do 11110001 111111110001 7 0111 00000111 000000000111 -7 1001 11111001 111111111001 8 can't do 00001000 000000001000 576 can't do can't do 001001000000 -576 can't do can't do 110111000000
There are few interesting things to notice about 2's complement numbers. First, all negative numbers will have a 1 in the first bit, just like sign-magnitude form. Thus it is immediately obvious if the number is negative or not. Second, some negatives are easy to spot. -1 is always all 1s, no matter how many bits long the numbers are. If we were working with a new 64-bit SPARC II chip, we would know that -1 is
1111111111111111111111111111111111111111111111111111111111111111without even trying to convert!
-2 is easy, too, since it is all 1s except for a final 0. -2 in 8 bits is 11111110, in 12 bits it is 111111111110. -4 is all 1s except for the final two 0s. In fact, there is an easy to spot pattern. -1 is always all 1s, which would normally be the largest unsigned binary number for n bits. From this point, the negative numbers count down or backwards.
Here is the complete list of representations for 3 bits using 2's complement:
Bit pattern Unsigned 2's complement ------------------------------------------- 100 4 -4 101 5 -3 110 6 -2 111 7 -1 000 0 0 001 1 +1 010 2 +2 011 3 +3
Notice that
These characteristics are true of 2's complement systems no matter how many bits there are. Let's examine a 16-bit system for comparison. We can determine that the largest positive number is 0111111111111111 which is 32,767. (An easy way to determine this is find 215 and subtract 1.) Thus we know the smallest negative is -32,768. There are 65,536 different numbers. The largest unsigned number will be 65,535.
Suppose that we are given a bit pattern 1001011100011110, which is 16 bits long. What is this number? Well, it could be an ASCII 2-byte character string! Or it could be an unsigned integer, which would be 38,686. But it might also be a sign-magnitude integer, in which case it would be -5918, or a 1's complement which would be -26849, or a 2's complement, which would be ... Well, how do we find out what it would be?
The answer is quite simple. A given bit pattern may be interpreted in a variety of ways, and we need to be told somehow what the interpretation is before we can correctly use it.
Given a bit pattern that you are told is a 2's complement number, you can find out its decimal equivalent in the following way. If the first bit is 0, then the number is positive and you simply convert the binary number to decimal. If the first bit is 1, the number is negative and we negate it to find its absolute value, since -(-x) = x. To negate any 2's complement number, Flip all the bits and add 1.
1001011100011110 (original) ?? 0110100011100001 (flip all bits) +1 (add 1) ----------------- 0110100011100010 (2's complement) 26,850
Since the number was original negative, the decimal equivalent of 1001011100011110 is -26,850.