Example: 5. Twos Complement
Example: 5. Twos Complement
Example: 5. Twos Complement
Twos complement
Adding a positive number and a negative number in binary can be awkward (even for a
CPU), but using the twos complement method makes it quite straightforward.
Example
Representing -5 in an 8 bit 2's complement format, carry out the following steps
1. Represent the number as if it was a positive 8 bit number, so this is 00000101
2. Keep the rightmost 1 but then flip all the bits to the left of it like this
00000101
keep the rightmost 1 and flip the rest
11111011
The pink 1 is in the 9th position, and so in an 8 bit register it is simply discarded. So you end
up with 00000010 which is 2 denary.
Example 2
Add -5 and -6 together using two's complement. The answer should be -11.
1. Convert -6 into 2's complement
+6 = 00000110
hold on to the rightmost 1 and flip the rest
-6 = 11111010
-11 = 11110101
Example 3
What does a twos complement 1001 represent as a denary number?
1. Work out what the positive number is by holding on to the rightmost 1 then flipping the
bits to the left of it, so it becomes 0111 which is 7. So twos complement 1001 is -7.
The largest number this can represent is 111.1 with a 111 exponent which is 7.5 x 2^7, but
fractionally you can only represent 0.5 any other fraction is not possible because you have
only provided 1 bit in this scheme. So this scheme is pretty hopeless in terms of precision.
So let's swap around the scheme slightly. This time we only allow 1 bit for the integer and 3
bits for the fractional part. Like this
This time you have three fractional bits to use so any combination of 1/2 , 1/4, 1/8 can be
used to describe a number, whilst the integer part can only be a 1 or a zero. Now the largest
number that can be represented is 1.111 x 2^7 which is not that much less than the 7.5 x 2^7
above. And yet we can now represent 0.001 binary which is 1/8. A good improvement in
precision.
If we want to represent say 6.0 then you use the exponent to move the binary point, like this
0 1.100 010
expand this out by moving the binary point by the exponent and you get 110.0 which is 6.0
decimal.
This trick of only allowing 1 bit for the integer part of a real number is called 'Normalisation'
Normalisation means that except for zero, a real number is represented with 1 integer and a
fractional part like this 1.fff
This scheme sacrifices a bit of range but gains significantly in precision.
A binary number is presented in this format, a 5 bit mantissa and 3 bit exponent
00111 011
Q: Is this in normalised form ?
Answer: No, because the first two bits are the same sign.
Q: Convert this into normalised form
Answer:
You want the first two bits to change sign and yet it must be the same number, so you must do
two things.
a) Shift the mantissa to the left until you see a 01 pattern, pad out the right with zeros as you
do so
b) For every shift to the left, decrease the exponent by 1.
Doing this will force the binary point to stay in the same place.
In its original form the number is 0.0111 with exponent 3, so the full number is
11.1
which is +3.5
Now if you normalise it, you shift the mantissa once to the left and becomes 0.1110 then
decrease the exponent by one so 011 becomes 010
Expanding this to its full number again we get
11.10
Note the greater precision because there are now 2 bits after the binary point
You use a standard method within the CPU to represent a real number