Lecture 1: Number Representation

advertisement
ECE 645: Lecture 1
Number Representation
Part 1
Required Reading
B. Parhami,
Computer Arithmetic: Algorithms and Hardware Design
Chapter 1, Numbers and Arithmetic, Sections 1.1-1.6
Chapter 2, Representing Signed Numbers
Recommended Reading
J-P. Deschamps, G. Bioul, G. Sutter,
Synthesis of Arithmetic Circuits: FPGA, ASIC and
Embedded Systems,
Chapter 1, Introduction
Chapter 3.1.1 Weighted systems
Chapter 3.2, Integers
Historical Representations:
Ancient Egyptians Numerals (1)
– ~4000 BC
– “Sum of Symbols”
= (34)10
Historical Representations:
Ancient Egyptians Numerals (2)
What number does this stone carving
from Karnak represent?
Historical Representations:
Ancient Egyptians Numerals (3)
Historical Representations:
Ancient Egyptians Numerals (4)
Symbol for a fraction (part):
Special symbols for most commonly used fractions:
Historical Representations:
Ancient Babylonian Numerals (1)
–
–
–
–
First known positional system
3100 BC
Radix 60 (sexagesimal)
Two symbols:
=1
= 10
− Integers and fractions were represented
identically - a radix point was not written but
rather made clear by context
Babylonian Numerals: Example
1 x 602
20 x 601
= (4,856)10
56 x 600
Historical Representations:
Ancient Babylonian Numerals (2)
Digits from 1 to 59
Positional Code with Zero
• Zero Represented by Space
– Partial solution
– What about trailing zeros?
• Babylonians Introduced New Symbol
–
or
– 4th to 1st Century BC
• Zero Allows Representation of Fractions
– Fractions started with zero
Mixed System
• Roman Numerals
– Sum of all symbols
– I=1 V=5 X=10 L=50 C=100 D=500 M=1000
– Difficult to do arithmetic
e.g.,
MCDXLVII
IX
?
Hindu-Arabic Numeral System
Brahmi numerals, India, 400 BC-400 AD
Evolution of numerals in early Europe
Positional Code Decimal System
– Documented in the 9th century
– Position of coefficient determines its value
– Coefficient in position is multiplied by radix
(10) raised to the power determined by its
position, e.g.,
4 * 103  8 * 102  5 * 101  6 * 100  4,85610
Migration of Positional Notation
• ~750 AD
– Zero spread from India to Arabic countries
• ~1250 AD
– Zero spread to Europe
• 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
01012  0 * 23  1* 22  0 * 21  1* 20  510
Modern Arithmetic and Number Systems
• Modern number systems used in digital arithmetic can be
broadly classified as:
• Fixed-point number representation systems
• Integers
• Rational numbers of the form x = a/2f, a is an integer, f is a positive integer
• Floating-point number representation systems
• x * bE, where x is a rational number, b the integer base, and E the exponent
• Note that all digital numbers are eventually coded in bits
{0,1} on a computer
17
Encoding Numbers in 4-Bits
16 14 12 10
8
6
4
2
0
2
4
6
8
10
12
14
16
Number
format
Unsigned integers
Signed-magnitude
fixed
point

3 + 1 fixed-point, xxx.x
Signed fraction, .xxx

2’s-compl. fraction, x.xxx
2 + 2 floating-point, s  2 e
e in [2, 1], s in [0, 3]
floating
e
s
point
2 + 2 logarithmic (log = xx.xx)
log x
18
Classification of number systems (1)
Number system
Non-positional
Positional
Fixed-radix
Conventional
Binary
Decimal
Hexadecimal
Mixed-radix
Unconventional
Signed-digit
Non-redundant
Redundant
Classification of number systems (2)
Positional
k 1
X   xi  wi
wi - weight of the digit xi
i  l
Fixed-radix
k 1
X   xi  r
i
r - radix of the number system
i  l
Conventional fixed-radix
k 1
X   xi  r
i  l
r integer, r > 0
i
xi  {0, 1, …, r-1}
Classification of number systems (3)
Unconventional fixed-radix
k 1
X   xi  r
i
xi  {-, …,  }
i  l
Signed-digit
Non-redundant
Redundant
>0  negative digits
number of digits =  +  + 1  r
number of digits =  +  + 1 > r
Fixed-point representation
Integral and fractional part
X = xk-1 xk-2 … x1 x0 . 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-Radix
Conventional (Unsigned)
Representations
Fixed-Radix Conventional Number Systems
Fixed Point Number system
Non-positional
Positional
Fixed-radix
Conventional
(unsigned)
Binary
Decimal
Hexadecimal
Mixed-radix
Unconventional (signed)
Signed-digit
Non-redundant
Redundant
24
Range of numbers
Xmin
Number system
Xmax
Decimal
X = (xk-1 xk-2 … x1 x0.x-1 … x-l)10
0
10k - 10-l
0
2k - 2-l
0
rk - r-l
Binary
X = (xk-1 xk-2 … x1 x0.x-1 … x-l)2
Conventional fixed-radix
X = (xk-1 xk-2 … x1 x0.x-1 … x-l)r
Notation:
ulp =
r-l
unit in the least significant position
unit in the last position
Number of digits
Number system
Number of digits in the integer
part necessary to cover the range
0..Xmax
Decimal
k  log 10 X max   1 
 log 10 ( X max  1)
Binary
k  log 2 X max   1 
 log 2 ( X max  1)
Conventional
fixed-radix
k  log r X max   1 
 log r ( X max  1)
Radix Conversion
Whole part
Fractional part
u = w.v
=
( xk–1xk–2 . . . x1x0 . x–1x–2 . . . x–l )r
= ( XK–1XK–2 . . . X1X0 . X–1X–2 . . . X–L )R
Old
New
Example: (31)eight = (25)ten
Two methods:
Option 1) Radix conversion, using arithmetic in the old radix r
Convenient when converting from r = 10 or familiar radix
Option 2) Radix conversion, using arithmetic in the new radix R
Convenient when converting to R = 10 or familiar radix
From: Parhami, Computer Arithmetic: Algorithms and Hardware Design
27
Option 1: Arithmetic in old radix r
Converting whole part w:
Repeatedly divide by five
(105)ten = (?)five
Quotient
Remainder
105
0
21
1
4
4
0
Therefore, (105)ten = (410)five
Converting fractional part v:
Repeatedly multiply by five
(105.486)ten = (410.?)five
Whole Part
Fraction
.486
2
.430
2
.150
0
.750
3
.750
3
.750
Therefore, (105.486)ten  (410.22033)five
From: Parhami, Computer Arithmetic: Algorithms and Hardware Design
28
Radix Conversion of the Integral Part
R - destination radix
k 1

XI = (xk-1 xk-2 … x1 x0)R =
xi  R
i
=
i 0
= ((...((xk-1 R + xk-2) R + xk-3 ) R + … + x2 ) R + x1 ) R + x0
Quotient
(...((xk-1 R + xk-2) R + xk-3 ) R + … + x2 ) R + x1
...((xk-1 R + xk-2) R + xk-3 ) R + … + x2
Remainder
x0
x1
……….
……….
xk-1
xk-2
xk-1
0
Radix Conversion of the Fractional Part
R - destination radix
1
XF = (. x-1 x-2 … x-l+1 x-l)R =

xi  R i
=
i  l
= R-1 (x-1 + R-1 (x-2 + R-1 (…. + R-1 ( x-l+1 + R-1 x-l )….)))
Integer part
x-1
x-2
Fractional part
R-1 (x-2 + R-1 (….. + R-1 ( x-l+1 + R-1 x-l)….))
R-1 (….. + R-1 ( x-l+1 + R-1 x-l)….)
………………………………….
x-l+1
x-l
R-1 x-l
...
Option 2: Arithmetic in new radix R
Converting fractional part v: (410.22033)five = (105.?)ten
(0.22033)five  55
=
(22033)five
=
(1518)ten
1518 / 55
=
1518 / 3125
=
0.48576
Therefore, (410.22033)five = (105.48576)ten
Converting (22033)five = (?)ten
((((2  5) + 2)  5 + 0)  5 + 3)  5 + 3
|-----|
:
:
:
:
10
:
:
:
:
|-----------|
:
:
:
12
:
:
:
|---------------------|
:
:
60
:
:
|-------------------------------|
:
303
:
|-----------------------------------------|
1518
From: Parhami, Computer Arithmetic: Algorithms and Hardware Design
Horner’s
rule or
formula
31
Option 2 cont'd: Horner's rule for fractions
Horner’s rule is also applicable: Proceed from right to
left and use division instead of multiplication
Converting fractional part v:
(0.22033)five = (?)ten
(((((3 / 5) + 3) / 5 + 0) / 5 + 2) / 5 + 2) / 5
|-----|
:
:
:
:
0.6
:
:
:
:
|-----------|
:
:
:
3.6
:
:
:
|---------------------|
:
:
0.72
:
:
|-------------------------------|
:
2.144
:
|-----------------------------------------|
2.4288
|-----------------------------------------------|
0.48576
From: Parhami, Computer Arithmetic: Algorithms and Hardware Design
Horner’s
rule or
formula
32
Radix Conversion Shortcut for r=bg, R=bG
r=bg 
b
 R=bG
4=22  2
 8=23
(2301.302)4 = (10 110 001. 110 010)2 = (261.62)8
• Trick here is to first convert to a number in radix b, then to R
• Cluster in groups of 3 (because 23 = 8) moving away from binary point
33
Signed Number Representations
Representations of signed numbers
Signed-magnitude
Biased
Radix-complement
r=2
Two’s complement
Complement
Diminished-radix
complement
(Digit complement)
r=2
One’s complement
Signedmagnitude
7
6
5
4
3
2
1
0
-0
-1
-2
-3
-4
-5
-6
-7
-8
0111
0110
0101
0100
0011
0010
0001
0000
1000
1001
1010
1011
1100
1101
1110
1111
Two’s
complement
One’s
complement
1111
1110
1101
1100
1011
1010
1001
1000
0111
0110
0101
0100
0011
0010
0001
0000
0111
0110
0101
0100
0011
0010
0001
0000
1111
1110
1101
1100
1011
1010
1001
1000
0111
0110
0101
0100
0011
0010
0001
0000
1111
1110
1101
1100
1011
1010
1001
1000
Biased
Signed-magnitude representation of signed numbers
k-1 k-2
0
magnitude
sign
Advantages:
• conceptual simplicity
• symmetric range: -(2k-1-1) .. -(2k-1-1)
• simple negation
Disadvantages:
• addition of numbers with the same sign and with
a different sign handled differently
Biased (excess-B) representation of signed integers
B = 2k-1, k=4
R(X) = X + B
-2k-1 ≤ X ≤ 2k-1-1
X
-8 -7 -6 -5 -4 -3 -2 -1 0 1
2 3 4 5 6 7
R
R(X)
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Biased representation
with radix 2
Signed number X
Representation
mapping
Unsigned Representation R(X)
Binary mapping
Bit vector (xk-1xk-2...x0.x-1...x-l)
k 1
R( X )   xi  2i
i  l
Complement
Signed Number Representations
Complement representations
with radix 2
Signed number X
Representation
mapping
Unsigned Representation R(X)
Binary mapping
Bit vector (xk-1xk-2...x0.x-1...x-l)
k 1
R( X )   xi  2i
i  l
Useful dependencies
1 – xi = xi
xi 1 – xi x i
0
1
1
0
1
0
X when X  0
|X| =
- X when X < 0
One’s complement transformation
For
k 1
A   Ai  2i  0
i  l
def
OC(A) = A = 2k – 2-l - A
k-1 k-2 ...
–
0
-1
-2 ... -l
Properties:
1 1 ... 1 . 1 1 ... 1
Ak-1 Ak-2 … A0 . A-1 A-2 ... A-l
0  OC(A)  2k – 2-l
Ak-1 Ak-2 … A0 . A-1 A-2 ... A-l
OC(OC(A)) = A
One’s Complement Representation
of Signed Numbers
For
–(2k-1 – 2-l)  X  2k-1 – 2-l
X
for X > 0
def
R(X) =
0 or OC(0) for X = 0
OC(|X|)
for X < 0
0  R(X)  2k – 2-l
One’s complement representation of signed integers
X>0
0
X
0,
2k-1
X<0
k=4
X+2k-1 = 2k-1 - |X|
-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
One’s complement representation of signed numbers
Two’s complement transformation (1)
For
k 1
A   Ai  2i  0
i  l
def
TC(A) =
A + 2-l = 2k – A for A > 0
0
2k – A = 2k – A – 2-l + 2-l =
for A = 0
Properties:
0  TC(A)  2k – 2-l
= (2k – 2-l – A)+2-l = A + 2-l
TC(TC(A)) = A
Two’s complement transformation (2)
For
k 1
A   Ai  2i  0
i  l
def
TC(A) = A + 2-l mod 2k = 2k – A mod 2k
Two’s Complement Representation
of Signed Numbers
For
–2k-1  X  2k-1 – 2-l
def
X
for X  0
TC(|X|)
for X < 0
R(X) =
0  R(X)  2k – 2-l
Two’s complement representation of signed integers
X>0
0
X
0
X<0
k=4
X+2k = 2k - |X|
-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
Two’s complement representation of signed integers
Signed-magnitude representation of signed numbers
X>0
0
X
0,
2k-1
X<0
k=4
| X|+2k-1 = -X+2k-1
-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
Signed-magnitude representation of signed numbers
Biased representation of signed numbers
X>0
0
X<0
X+B
B
X+B
-8 -7 -6 -5 -4 -3 -2 -1 0 1
B = 2k-1, k=4
2 3 4 5 6 7
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Biased representation of signed numbers
Arithmetic Operations in
Signed Number Representations
Unsigned addition vs. signed addition
Programmer
Machine
128 64 32 16 8 4
weight
carry
Unsigned
mind
2 1
Signed
mind
1 1 1
0 0 0 1 0 0 1 1
1 0 0 0 0 1 0 1
1 0 0 1 1 0 0 0
X
+Y
=S
x6 y6
x7 y7
c8
FA
s7
c7
FA
s6
x5 y5
c6
FA
s5
x3 y3
x4 y4
c5
FA
s4
c4
FA
s3
x2 y2
c3
FA
s2
x1 y1
c2
FA
s1
x0 y0
c1
FA
s0
Out of range flags
Carry flag - C
C=1
0
out-of-range for unsigned numbers
if
result > MAX_UNSIGNED or
result < 0
otherwise
where MAX_UNSIGNED = 28-1 for 8-bit operands
216-1 for 16-bit operands
Overflow flag - V
V=1
0
out-of-range for signed numbers
if
result > MAX_SIGNED or
result < MIN_SIGNED
otherwise
where MAX_SIGNED = 27-1 for 8-bit operands
215-1 for 16-bit operands
MIN_SIGNED = -27 for 8-bit operands
-215 for 16-bit operands
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-1
Two’s complement representation of signed integers
Addition and subtraction
Two’s complement
Numbers of the same sign
-16 8 4 2 1
Numbers of the opposite sign
-16 8 4 2 1
11011
10110
-5
-10
00101
10110
5
-10
110001
-15
11011
-5
carry but not overflow
-16 8 4 2 1
00111
01010
7
10
10001
-15
no carry but overflow
-16 8 4 2 1
01010
11011
100101
10
-5
5
carry but not overflow
Two's Complement Adder/Subtractor
Architecture
Can replace this
with k xor gates
Arithmetic Shift
Two’s complement
Sh.L {001012 = +5} = 010102 = +10
Sh.L {110112 = -5} = 101102 = -10
Sh.L {010102 = +10} = 101002 = - 12
Shift left may cause overflow
overflow
Sh.R {001012 = +5} = 000102 = +2
rem 1
Sh.R {110112 = -5} = 111012 = -3
rem 1
Shift right requires sign extension
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
11010
10101
+
-5
-10
101111
1
10000
end-arround carry
+
-15
01010
11010
100100
1
10
-5
00101
5
Disadvantage: Need another adder after
the addition is complete!
Addition and subtraction
Signed-magnitude
Numbers of the same sign
sign bit
+
0
0
Numbers of the opposite sign
magnitude
1011
0110
11
6
0 10001
17
carry = overflow
sign bit
+
1
0
magnitude
1011
0110
-11
6
11 > 6
–
1
1011
0110
11
6
0101
5
Signed Number Representations
Summary
Representing k-bit signed binary numbers
Representation
Representation Representation
for X>0
for 0
Signedmagnitude
X
0,
2k-1
Biased
X+B
B
Representation
for X<0
2k-1+|X|
X+B
typical B=2k-1 or 2k-1-ulp
Complement
X
Two’s
complement
X
One’s
complement
X
0, M mod 2k
0
0, 2k-ulp
M-|X|=M+X
2k-|X|= X  ulp
2k-ulp-|X|= X
Value of a number in the signed representations
Representation
Signedmagnitude
Value of
(xk-1 xk-2 … x1 x0.x-1 … x-l)
X  ( 1)
x k 1
k 2
i
x

2
i
i  l
k 1
Biased
Two’s
complement
X   xi  2i  B
i  l
X   xk 1 2
k 1
k 2
  xi  2i
i  l
k 2
One’s
complement
X   xk 1 (2k 1  ulp )   xi  2i
i  l
Extending the number of bits of a signed number
xk-1 xk-2 … x1 x0 . x-1 x-2 … x-l
X
Y
yk’-1 yk’-2 … yk yk-1 yk-2 … y1 y0 . y-1 y-2 … y-l y-(l+1) … y-l’
signed-magnitude
xk-1 0 0 0 0 0 0 0 xk-2 … x1 x0 . 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 … x1 x0 . 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 … x1 x0 . x-1 x-2 … x-l xk-1 . . . .xk-1
biased
xk-1 x k 1 . . . x k 1 xk-2 … x1 x0 . x-1 x-2 … x-l 0 0 0 0 0 0
Generalized Complement Representation
Generalized complement representation
of signed integers
M-N > P
M ≥ N+P+1
Generalized complement representation
of signed integers
Download