Converting to Two's Complement | |||||||||||||||||||||||||||||||||||||||||||||||||||
We have seen how two's complement can be used to represent negative numbers. We now need to look at how we can convert a positive number into a negative. Let's start off with an example, to convert 6 to -6. First of all we write down the binary representation of 6. The numbers in the top row represent the column values for each binary digit. So 4 + 2 = 6. Starting from the right, we copy all of the binary digits until we see a 1 as shown in the diagram above. These are then copied over to the answer. In this case we copy over the bits 10, as this is the first 1 bit we read in from the right hand side. Finally we simply complement, or flip the bits, of the rest.
The above table shows the answer worked through. The shaded values are the bits which are complemented. Let us look at the same table for 3 and 4.
So the algorithm we follow is.
In order to convert a negative number to a positive number, we follow the exact same algorithm.
|