Example: 5. Twos Complement

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

5.

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

So 11111011 is -5 in twos complement format


To show how you can add 7 and -5 together the answer should be 2. So let's do it. +7 is
00000111
00000111
11111011
---------100000010

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

2. As shown above -5 in 2's complement is 11111011


3 Add the two numbers together
11111010
11111011
---------111110101
Is this the right answer? Lets work out -11 in twos complement
+11 = 00001011
hold on to the rightmost 1 and flip the rest

Which is the right answer.

-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.

10. Normalised Floating point


We want the floating point system to represent as wide a range of real numbers with as much
precision as possible.
Don't forget, only a fixed number of bits are available in any given scheme (8 bit, 16 bit, 32
bit, 64 bit and so on).
For example, say you want to use an 8 bit scheme. Say, 3 bits for the exponent, 1 bit for the
sign, 3 bits for numbers greater than 1 which only leaves 1 bit for a fraction. Like this

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.

Tip: Spotting a normalised number.


Which of these are normalised numbers (8 bit scheme, 3 bit exponent, uses twos
complement)
1. 00110 011
2. 01100 010
Answer: If the left 2 bits change sign, then that indicates the number is normalised.
In the example above, they can both represent 3 decimal. But the first one is not normalised
but the second one is normalised.
In the first example the binary number is 0.0110 and the exponent is 3 so move the binary
point three places to the right. You get 11.0 which is 3 decimal
In the second example the binary number is 0.1100 with the exponent 2 so move the binary
point two places to the right and you still get 11.00 which is once again 3 decimal. But note
that you now have 2 binary bits after the point, which indicates you have more precision
available.
11. Converting to Normalised form
Example

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

12. Reasons to use Normalised form

You use a standard method within the CPU to represent a real number

Normalisation provides the maximum precision with the bits available

Multiplication is more accurate as there is less truncation of decimal numbers

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy