02-Signed Number Systems
Download
Report
Transcript 02-Signed Number Systems
Radix Conversion
Given a value X represented in source system with radix s,
represent the same number in a destination system with radix d
Consider the integral part of the number, XI, in the d system:
X I xk 1 d
k 1
xk 2 d
k 2
x1 d x0 d
{[( xk 1 d xk 2 ) d
1
0
x2 ] d x1} d x0
0 xi d
If XI is divided by d , we obtain x0 as a remainder and quotient
Q {[( xk 1 d xk 2 ) d
x2 ] d x1}
R x0
R is the Desired digit (LSD) – Can Repeatedly Divide to Obtain
Converted Value
Radix Conversion Example
XI = 34610
s=10
d =3
Fixed-point Decimal to Ternary Integer Conversion
XI = 1102113
Check by evaluating the radix polynomial
1 35 1 34 0 33 2 32 1 31 1 30
[243 81 18 3 1]10 34610
Radix Conversion (fractional)
Consider the fractional part of the value in d Fixed point system
X F x1 d 1 x2 d 2
x ( m 1) d ( m 1) x m d m
d 1{x1 d 1[ x2 d 1 ( x3
)]}
d X F PI PF
PI x1
PF d 1[ x2 d 1 ( x3
)]
Thus, PI is the Desired Digit
We can Repeatedly Multiply by the d Value
Radix Conversion Example
XI = 0.29110
s=10
d =5
Fixed-point Decimal to Pentary Fractional Conversion
0.291 5 1.455 1
0.455 5 2.275 2
0.275 5 1.375 1
0.375 5 1.875 1
0.875 5 4.375 4
0.375 5 1.875 1
0.875 5 4.375 4
0.29110 (0.12114141414 )5
0.29110 is Finite Fraction for s=10, but infinite fraction for d =5
Fixed Point Negative Numbers
Two Common Forms:
1. Signed-magnitude Form
2. Complement Forms
Signed Magnitude
•
•
•
•
First Digit is Sign Digit, Remaining n-1 are the Magnitude
Convention (binary)
– 0 is a Positive Sign bit
– 1 is a Negative Sign bit
Convention (non-binary)
– 0 is a Positive Sign digit
– -1 is a Negative Sign digit
Only 2• n-1 Digit Sequences are Utilized
Signed Magnitude Example
n7
k 4
k 1 3
m3
s 2
d 10
1101.1102 1 (1 2 2 1 20 1 2 1 1 2 2 )10
1 1
1 4 1 5.7510
2 4 10
Largest Representable Value is:
0111.1112 1 (1 22 1 21 1 20 1 21 1 22 1 2 3 )10
1 1 1
7
1 4 2 1 7
2 4 8 10
8 10
Signed Magnitude Example (cont)
n7
k 4
k 1 3
m3
s 2
d 10
0111.111 1 (1 22 1 21 1 20 1 2 1 1 2 2 1 2 3 )10
1 1 1
7
1 4 2 1 7
2 4 8 10
8 10
1
1 ulp 1 2
8
3
k 1 3,
s k 1 23 8
1
1 ulp ( s )
8
X MAX k 1 ulp
X MIN 1 ( k 1 ulp )
X [ X MIN , X MAX ]
m 1
Signed Magnitude Ternary Example
n7
k 4
k 1 3 m 3
xi {0,1,2}
s 3
d 10
2102.120 1 (1 32 2 30 1 31 2 32 )10
1 2
5
1 9 2 11
3 9 10
9 10
(11.555555 )10
Notice that fractional part is infinite in =10 but finite in =3
1
m 1
1 ulp min{xi 0} ( s )
27
X MAX k 1 ulp
X MIN 1 ( k 1 ulp )
X [ X MIN , X MAX ]
Signed Magnitude Ternary Bounds
n7
k 4
k 1 3 m 3
xi {0,1,2}
s 3
d 10
X MAX 0( 1)( 1)( 1).( 1)( 1)( 1)
0222.2223 1 (2 32 2 31 2 30 2 31 2 32 2 33 )10
2 2 2
26
1 18 6 2 26
3 9 27 10
27
1
k 1
ulp 27
27
k 1
[
0,
ulp]
Positive Numbers:
k 1
Negative Numbers: [( ulp), 0]
Range:
[( k 1 ulp), k 1 ulp]
Signed Magnitude Comments
• Two Representations for zero, +0 and –0
• Addition of +K and –K is not zero
EXAMPLE
10001010.002
+00001010.002
10010100.002
-1010+1010 Yields a Sum of –2010!!!!!
Complement Representations
•
Two Types of Complement Representations
1. radix complement (binary – 2’s complement)
2. diminished radix complement (binary – 1’s complement)
•
Positive Values Represented Same Way as Signed
Magnitude for Both Types
•
Negative Value, -Y, Represented as R-Y Where R is a Constant
•
Obeys the Identity: (Y ) R ( R Y )
R R Y
Y
• Advantage is No Decisions Needed Based on Operand
Sign Before Operations are Applied
Complement Representation Example
•
X is Positive, Y is Negative, Compute X+Y
Using Complement Representation
X ( R Y ) [ X ( R Y )]
[ R Y X ]
R (Y X )
•
If |Y|>X, Then the Answer is R-(Y-X)
•
If X>|Y|, Then the Answer Should be X-Y
– But X+(R-Y)=R+(X-Y), Thus R Must be Discarded!
•
Solution is to Choose the Value of R Carefully
Requirements for Complementation Value, R
•
Select R to Simplify (or Eliminate) Correction for
the X>|Y| Case
•
Calculation of Complement of Y, (R-Y) Should be
Simple and Fast
•
Definition of Complement for Single Digit, xi
xi ( 1) xi
•
Definition of Complement for a Word, X
X ( xk 1xk 2
x m )
Complementation Value, R
•
Add Word and Complement Together:
X ( xk 1
xk 2
x m )
X ( xk 1
xk 2
x m )
( 1)( 1)
ulp
0
0
1 0
0
Now Add
1 ulp
•
( 1)
1
0
Therefore, we see that:
X X ulp k
k X X ulp
Answer to
Addition
Radix Complement Form
•
The Radix Complement Form is Defined When:
R k
R X k X X ulp
•
Using k is Convenient Since Storing Result in Register of
Length n Causes MSD of 1 to be Discarded due to Finite
Register Length
•
Therefore, it is Easy to Compute the Complement of X by:
1. Take the Complement of X
2. Add 1ulp to Complement
X X ulp k
k X X ulp
Radix Complement Form (cont)
•
No Correction is Needed When We have Positive X and
Negative Y Such That:
X (R Y ) 0
•
Since R= k
X (R Y ) R ( X Y )
k X Y
•
And k is discarded Due to Finite Register Length
Radix Complement Example
2
k n4
•
Since n = m + k m=0
•
Therefore 1 ulp = 20=1
•
Given X, the radix complement (2’s complement) is:
R X k X X ulp X 1
•
Range of Positive Numbers is [0000,0111]
•
2’s Complement of Largest, 0111:
X 1000;
•
X 1 10012 710
In Radix Complement, There is a Single Representation of Zero
(0000) and Each Positive Number has Corresponding Negative
Number With MSB=1
Radix Complement Example
2
•
k n4
In Radix Complement, There is a Single Representation of Zero
(0000) and Each Positive Number has Corresponding Negative
Number With MSB=1
•
Accounts for 1(zero)+7(pos.)+7(neg.), But Extra Bit Pattern Left
•
One Additional Negative Number, 10002=-810,
0010 (210 )
0111 (710 )
1001 ( 710 )
1110 ( 210 )
0 1011 ( 510 )
1 0101 (510 )
-810X+710
1011 0100 0100 1 0101 ( decode)
Same to Encode
Diminished Radix Complement
2
•
k n4
In Diminished Radix Complement, the Complementation
Process is Easier Since the Addition of 1 ulp is Avoided
R k ulp
R X k ulp X X
•
Range of Positive Numbers is: [00002,01112]=[010,710]
•
1’s Complement of Largest is 10002= -710
•
1’s Complement of Zero is 11112
•
Two Representations of Zero!
7 X 7
•
In All Cases MSB is Sign Bit
Comparison of Two’s Complement, One’s
Complement and Signed-Magnitude
Sequence
011
010
001
000
111
110
101
100
Two’s
One’s
Complement Complement
3
3
2
2
1
1
0
0
-1
-0
-2
-1
-3
-2
-4
-3
SignedMagnitude
3
2
1
0
-3
-2
-1
-0