Signed Numbers

Download Report

Transcript Signed Numbers

Signed Numbers



Up till now we've been concentrating on unsigned
numbers. In real life we have to represent signed numbers
( like: -12, -45, 78).
The difference between signed and unsigned numbers is
the sign. A scheme is needed to represent the sign as part
of the binary representation.
There are a number of schemes for representing signed
numbers in binary format.
 sign-magnitude representation
 the twos-complement representation.
Sign-Magnitude Representation
In this representation, the leftmost bit of a binary
code represents the sign of the value:


0 for positive,
1 for negative;
the remaining bits represent the numeric value.
Sign-Magnitude Representation
To Compute negative values using
Sign/Magnitude (signmag) representation.
Begin with the binary representation of the
positive value, then flip the leftmost zero
bit.
Sign-Magnitude Representation
Ex 1. Find the signmag representation of -610
Step1: find binary representation using 8 bits
610 = 000001102
Step2: if the number is a negative number flip left most
bit
10000110
So:
-610 = 100001102 (in 8-bit sign/magnitude form)
Sign-Magnitude Representation
Ex 2. Find the signmag representation of -3610
Step 1: find binary representation using 8 bits
3610 = 001001002
Step 2: if the number is a negative number flip left most
bit
10100100
So: -3610 = 101001002 (in 8-bit sign/magnitude form)
Sign-Magnitude Representation
Ex 3. Find the signmag representation of 7010
Step 1: find binary representation using 8 bits
7010 = 010001102
Step 2: if the number is a negative number flip left most
bit
01000110
(no flipping, since it is +ve)
So: 7010 = 010001102 (in 8-bit sign/magnitude form)
Sign-Magnitude Representation
What is this signmag number?
100000002
The machine will think of it as - 0 , which
is a non valid value.
Two’s Complement Representation


Another scheme to represent negative
numbers
The leftmost bit serves as a sign bit:


0 for positive numbers,
1 for negative numbers.
Two’s Complement Representation
To Compute negative values using two’s
Complement representation, begin with the
binary representation of the positive value,
complement (flip each bit if it is 0 make it
1 and visa versa) the entire positive
number, and then add one.
Two’s Complement Representation
Ex. Find the two’s complement representation of
–610
Step1: find binary representation in 8 bits
610 = 000001102
Two’s Complement Representation
Step 2: Complement the entire positive number, and
then add one
(complemented)
(add one) ->
->
+
00000110
11111001
1
11111010
So: -610 = 111110102 (in 2's complement form,
using any of above methods)
Two’s Complement Representation
Alternative method for step 2
Scan binary representation from right too left,
find first one bit, from low-order (right) end, and
complement the remaining pattern to the left.
(left complemented)
-->
00000110
11111010
Two’s Complement Representation
Ex 2: Find the Two’s Complement of -7610
Step 1: Find the 8 bit binary representation of
the positive value.
7610 = 010011002
Two’s Complement Representation
Step 2:
Find first one bit, from low-order (right) end, and
complement the pattern to the left.
(left complemented)
->
01001100
10110100
So: -7610 = 101101002 (in 2's complement form,
using any of above methods)
Two’s Complement Representation
Ex 3: Find the Two’s Complement of 7210
Step 1: Find the 8 bit binary representation of
the positive value.
7210 = 010010002
Two’s Complement Representation
Step 2:
Since number is positive do nothing.
So: 7210 = 010010002 (in 2's complement
form, using any of above methods)
Two’s Complement Representation
The most important characteristic of the
two’s-complement system is that the binary
codes can be added and subtracted as if
they were unsigned binary numbers,
without regard to the signs of the numbers
they actually represent.
Two’s Complement Representation
For example, to add +4 and -3, we simply add the
corresponding binary codes, 0100 and 1101:
0100 (+4)
+1101 (-3)
0001 (+1)
A carry from the leftmost column has been
ignored. The result, 0001, is the code for +1, the
sum of +4 and -3.
Twos Complement Representation
Likewise, to subtract +7 from +3, we add the
code for -7, 1001, to that of +3, 0011:
0011 (+3)
+1001 (-7)
1100 (-4)
The result, 1100, is the code for -4, the result of
subtracting +7 from +3.
Two’s Complement Representation
Benefits of Twos Complements:


addition and subtraction simplified in the
two’s-complement system,
In 8 bits, -0 has been eliminated, replaced by
-128, for which there is no corresponding
positive number.