Download Numerical Representation in Computer Arithmetic - Lecture Slides | ECE 645 and more Study notes Electrical and Electronics Engineering in PDF only on Docsity!
Number Representation
ECE 645: Lecture 1
Required Reading
Chapter 1, Numbers and Arithmetic,
Sections 1.1-1.6, pp. 3-
Chapter 2, Representing Signed Numbers,
Sections 2.1-2.6, pp. 19-
Behrooz Parhami,
Computer Arithmetic: Algorithms and Hardware Design
Codes for Numbers
- Egyptian
- ~4000 BC
- “Sum of Symbols”
Positional Code for Numbers
- Babylonians
- Positional system
- 2000 BC
- Radix 60
- = 1 = 10
Babylonian Example
1 x 60^2 20 x 60^1 56 x 60^0
Positional Code with Zero
- Zero Represented by Space
- Partial solution
- What about trailing zeros?
- Babylonians Introduced New Symbol
- Zero Allows Representation of Fractions
- Fractions started with zero
Migration of Positional
Notation
- ~750 AD
- Zero spread from India to Arabic countries
- ~1250 AD
- Importance of Zero
- Ease of arithmetic which leads to improved
commerce
Binary Number System
- Binary
- Positional number system
- Two symbols, B = { 0, 1 }
- Easily implemented using switches
- Easy to implement in electronic circuitry
- Algebra invented by George Boole (1815-1864)
allows easy manipulation of symbols
Number system
Positional Non-positional
Fixed-radix Mixed-radix
Conventional Unconventional
Signed-digit
Non-redundant Redundant
Binary
Decimal
Hexadecimal
Classification of number systems (1)
Classification of number systems (2)
Positional
wi - weight of the digit xi
Fixed-radix
i
k
i l
X = xi ⋅ w
=−
1
i
k
i l
X = xi ⋅ r
−
=−
1
r - radix of the number system
Conventional fixed-radix
i
k
i l
X = xi ⋅ r
−
=−
1 r^ integer,^ r^ > 0
x i ∈ {0, 1, …, r -1}
Classification of number systems (3)
Unconventional fixed-radix
i
k
i l
X = xi ⋅ r
−
=−
1
x i ∈ {-α, …, β }
Non-redundant number of digits = α + β + 1 ≤ r
Redundant number of digits = α + β + 1 > r
Signed-digit α>0 negative digits
Integral and fractional part
X = xk -1 xk -2 … x 1 x 0. x -1 x -2 … x - l
Integral part Fractional part
Radix point
- NOT stored in the register
- understood to be in a fixed position
Fixed-point representation
Radix Conversion of the Fractional Part
XF = (. x -1 x -2 … x - l +1 x - l )R = =
R - destination radix
i
il
∑ xi^ ⋅ R
−
=−
1
= R-1^ ( x-1 + R-1^ ( x-2 + R-1^ (…. + R-1^ ( x-l+1 + R-1^ x-l )….)))
Integer part Fractional part
x-1 R-1^ ( x-2 + R-1^ (….. + R-1^ ( x-l+1 + R-1^ x-l )….))
x-2 R-1^ (….. + R-1^ ( x-l+1 + R-1^ x-l )….)
x-l+1 R-1 x
-l
x-l ...
Shortcut for r=bg, R=bG
r=bg^ →→→→ b →→→→ R=bG
4=2^2 →→→→ 2 →→→→ 8=2^3
Signed Number Representations
Representations of signed numbers
Signed-magnitude Biased^ Complement
Radix-complement Diminished-radix
complement
(Digit complement)
Two’s complement One’s complement
r =
r =
7 0111 1111 0111 0111 6 0110 1110 0110 0110 5 0101 1101 0101 0101 4 0100 1100 0100 0100 3 0011 1011 0011 0011 2 0010 1010 0010 0010 1 0001 1001 0001 0001 0 0000 1000 0000 0000 -0 1000 1111 -1 1001 0111 1111 1110 -2 1010 0110 1110 1101 -3 1011 0101 1101 1100 -4 1100 0100 1100 1011 -5 1101 0011 1011 1010 -6 1110 0010 1010 1001 -7 1111 0001 1001 1000 -8 0000 1000
Signed-
magnitude
Biased Two’s
complement
One’s
complement
Signed-magnitude representation of signed numbers
Advantages:
Disadvantages:
- conceptual simplicity
- symmetric range: -(2k-1-1) .. -(2k-1-1)
- simple negation
- addition of numbers with the same sign and with
a different sign handled differently
k-1k-2 0
sign
magnitude
Signed number X
Unsigned Representation R( X )
Bit vector ( xk -1 xk -2... x 0. x -1... x-l )
Binary mapping
Representation
mapping
k i
i l
R ( X ) xi 2
1
−
=−
Complement representations with radix 2
1 – xi = xi
xi 1 – xi xi
Useful dependencies
|X| =
X when X ≥ 0
One’s complement transformation
OC( A ) = A = 2 k^ – 2- l^ - A
For i k
i l
A Ai 2
1 ⋅
=
−
=−
≥ 0
0 ≤ OC( A ) ≤ 2 k^ – 2- l
OC(OC( A )) = A
def
k -1 k -2 ... 0 -1 -2 ... - l
- A k -1 A k -2 … A 0. A-1 A-2 ... A- l
A k -1 A k -2 … A 0. A-1 A-2 ... A- l
Properties:
One’s Complement Representation of Signed Numbers
R(X) =
X for X > 0
0 or OC(0) for X = 0
OC(|X|) for X < 0
For –(2 k-1^ – 2 -l ) ≤ X ≤ 2 k-1^ – 2 -l
0 ≤ R(X) ≤ 2 k^ – 2 -l
def
One’s complement representation of signed integers
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
X
k =
X >0 0 X <
0, X +2 k -1 = 2 k -1 - | X |
2 k -
One’s complement representation of signed numbers
Two’s complement representation of signed integers
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
X
k =
X >0 0 X <
0 X +2 k^ = 2 k^ - | X |
Two’s complement representation of signed integers
Signed-magnitude representation of signed numbers
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
X
k =
X >0 0 X <
0, | X| +2 k -1 = -X +2 k -
2 k -
Signed-magnitude representation of signed numbers
Biased representation of signed numbers
-8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
X + B
B = 2 k -1, k =
X >0 0 X <
B X + B
Biased representation of signed numbers
Overflow for signed numbers
Indication of overflow
Positive
+ Positive
= Negative
Negative
+ Negative
= Positive
Formulas
Overflow2’s complement = xk-1 yk-1 sk-1 + xk-1 yk-1 sk-1 =
= ck ⊕ ck-
Two’s complement representation of signed integers
Addition and subtraction
Two’s complement
-16 8 4 2 1
Numbers of the same sign Numbers of the opposite sign
-16 8 4 2 1
carry but not overflow
-16 8 4 2 1
carry but not overflow
-16 8 4 2 1
no carry but overflow
Arithmetic Shift
Two’s complement
Sh.L {00101 2 = +5} = 01010 2 = +
Sh.L {11011 2 = -5} = 10110 2 = -
Sh.L {01010 2 = +10} = 10100 2 = - 12
overflow
Sh.R {00101 2 = +5} = 00010 2 = +2 rem 1
Sh.R {11011 2 = -5} = 11101 2 = -3 rem 1
Addition and subtraction
One’s complement
Numbers of the same sign Numbers of the opposite sign
-15 8 4 2 1
-15 8 4 2 1
end-arround carry
Signed Number Representations
Summary
Representing k-bit signed binary numbers
Representation
for X >
Representation
for X <
Representation
for 0
Representation
Signed-
magnitude
X
2 k -^
2 k -1+| X |
Biased X + B B X + B
Complement
X 0, M mod 2 k M -| X |= M + X
Two’s
complement
One’s
complement
X
X
2 k -| X |=
2 k - ulp -| X |=
0, 2 k - ulp
typical B =2 k -1^ or 2 k -1- ulp
X + ulp
X
Value of a number in the signed representations
Representation
Value of
( xk -1 xk -2 … x 1 x 0. x -1 … x - l )
Signed-
magnitude
Biased
Two’s
complement
One’s
complement
i k
i l
i
x
X ( 1 ) k x 2
2 = −^1 ⋅
−
=−
−
X x i B
k
i l
= i ⋅ −
−
=−
1
i k
i l
i
k
X xk 2 x 2
2 1 = − 1 + ⋅
−
=−
− −
i k
i l
i
k
X xk ( 2 ulp ) x 2
2 1 = − 1 − + ⋅
−
=−
− −
Extending the number of bits of a signed number
xk -1 xk -2 … x 1 x 0. x -1 x -2 … x - l
yk’ -1 yk’ -2 … yk yk -1 yk -2 … y 1 y 0. y -1 y -2 … y - l y -( l +1) … y - l’
X
Y
signed-magnitude
xk -1 0 0 0 0 0 0 0 xk -2 … x 1 x 0. x -1 x -2 … x - l 0 0 0 0 0 0
two’s complement
xk -1 xk -1 xk -1... xk -1 xk -2 … x 1 x 0. x -1 x -2 … x - l 0 0 0 0 0 0
one’s complement
xk -1 xk -1 xk -1... xk -1 xk -2 … x 1 x 0. x -1 x -2 … x - l xk -1.... xk -
biased
xk -1 x k − 1... xk − 1^ xk -2 …^ x 1 x 0.^ x -1 x -2 …^ x - l 0 0 0 0 0 0
Generalized Complement Representation
Generalized complement representation of signed integers