Data Types and Representations - IUMA - ULPGCnunez/clases-FdC/irvine-gajski... · 2006-10-11 ·...
Transcript of Data Types and Representations - IUMA - ULPGCnunez/clases-FdC/irvine-gajski... · 2006-10-11 ·...
Principles OfPrinciples Of
Digital DesignDigital DesignChapter 2Chapter 2
Data Types and Representations
Decimal, Binary, Octal, and Hexadecimal Number System Complement Number SystemFixed Point and Floating Point NumbersCharacter Codes, Cube Representation
2Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Each number is represented by a string of digits, in which the position of each digit has an associated weight
1234.56 = 1 1234.56 = 1 ·· 1000 + 2 1000 + 2 ·· 100 + 3 100 + 3 ·· 10 + 4 10 + 4 ·· 1 + 5 1 + 5 ·· 0.1 + 6 0.1 + 6 ·· 0.010.01
In general, any decimal number D of the form
has the value
Positional Number SystemPositional Number System
ddm m –– 11 ddm m –– 22 ……dd11 dd00 .d.d––11 dd––22 ……dd––nn
Least significant digitRadix pointMost significant digit
DD = = ddm m –– 11 ·· 1010m m –– 11 ++ ……+ + dd00 ·· 101000 + + dd––11 ·· 1010––11 + + ……+ + dd––nn ·· 1010––nn == ∑−
−=
⋅1m
ni
ii rd∑
−
−=
⋅1m
ni
ii rd
Radix
3Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
General form of a binary number:
Its value is equivalent to
Examples
Binary Number SystemBinary Number System
bbmm –– 11 bbmm –– 22 ……bb11 bb00 bb––11 bb––22 ……bb––nn
Least significant bit (LSB)Binary pointMost significant bit (MSB)
B =B = ∑−
−=
⋅1
2m
ni
iib∑
−
−=
⋅1
2m
ni
iib
Radix 2
101011010122 = 1 ·· 16 + 0 16 + 0 ·· 8 + 1 8 + 1 ·· 4 + 0 4 + 0 ·· 2 + 1 2 + 1 ·· 1 = 211 = 211010
11010111010122 = 1 = 1 ·· 32 + 1 32 + 1 ·· 16 + 0 16 + 0 ·· 8 + 1 8 + 1 ·· 4 + 0 4 + 0 ·· 2 + 1 2 + 1 ·· 1 = 531 = 531010
10.10110.10122 = 1 = 1 ·· 2 + 0 2 + 0 ·· 1 + 1 1 + 1 ·· 0.5 + 0 0.5 + 0 ·· 0.25 + 1 0.25 + 1 ·· 0.125 = 2.6250.125 = 2.6251010
0.11110.111122 = 1 = 1 ·· 0.5 + 1 0.5 + 1 ·· 0.25 + 1 0.25 + 1 ·· 0.125 + 1 0.125 + 1 ·· 0.0625 = 0.93750.0625 = 0.93751010
4Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Octal and Hexadecimal NumbersOctal and Hexadecimal Numbers
16Hexadecimal
10Decimal
8Octal
2Binary
RadixNumbers
F15171111E14161110
777111666110555101444100333112221011110000
B11131011A101210109911100188101000
1000110000
11011100
Binary
2120
1514
Octal
11171016
D13C12
HexadecimalDecimal
Different representation of numbers 0 through 21Back to Conversion of Numbers (Part II)Back to Conversion of Numbers (Part II)
5Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Conversion of NumbersConversion of Numbers(Binary, Octal, Hexadecimal)(Binary, Octal, Hexadecimal)
Binary to Octal ConversionStart at the binary point and work left. Separate the bits intoStart at the binary point and work left. Separate the bits into groups groups of three, replace each group with corresponding octal digit.of three, replace each group with corresponding octal digit.
Binary to Hexadecimal ConversionStart at the binary point and work left. Separate the bits intoStart at the binary point and work left. Separate the bits into groups groups of four, replace each group with corresponding hexadecimal digitof four, replace each group with corresponding hexadecimal digit..
1010011100101001110022 = 001 010 011 100 = 1234= 001 010 011 100 = 123488
1010011100101001110022 = 0010 1001 1100 = 29C= 0010 1001 1100 = 29C1616
6Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Conversion of FractionsStart at the binary point. Group the binary digits on the rightStart at the binary point. Group the binary digits on the right into into groups of three (Octal) or four (Hexadecimal).groups of three (Octal) or four (Hexadecimal).
Conversion to Binary NumbersReplace each octal or hexadecimal digit with corresponding 3Replace each octal or hexadecimal digit with corresponding 3--bit or bit or 44--bit binary string from the bit binary string from the Octal and Hexadecimal TableOctal and Hexadecimal Table..
Conversion of NumbersConversion of Numbers(Binary, Octal, Hexadecimal)(Binary, Octal, Hexadecimal)
0.101110.1011122 = 0.101 110 = 0.101 110 = 0.56= 0.5688= 0.1011 1000 = 0.B8= 0.1011 1000 = 0.B81616
76576588 == 11111010111111010122765.432765.43288 == 111110101.100011010111110101.10001101022
FEDFED1616 = = 11111110110111111110110122FED.CBAFED.CBA1616 = = 111111101101.110010111010111111101101.11001011101022
7Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Conversion to Decimal NumbersConversion to Decimal Numbers
DD = = = ((= ((……((((ddm m –– 11))r +r + ddm m –– 22))rr ++ ……))rr + + dd11))rr + + dd00∑−
=
⋅1
0
m
i
ii rd∑
−
=
⋅1
0
m
i
ii rd S = S ·· r + di
i = m – 1
S = 0
Start
i = 0
i = i – 1
Done
no
yes
8Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Dividing the top equation by r, we obtain the quotient Q and remainder R
Conversion from Decimal NumbersConversion from Decimal Numbers
DD = = = ((= ((……((((ddm m –– 11))r +r + ddm m –– 22))rr ++ ……))rr + + dd11))rr + + dd00∑−
=
⋅1
0
m
i
ii rd∑
−
=
⋅1
0
m
i
ii rd
QQ = (= (……((((ddm m –– 11))r +r + ddm m –– 22))rr ++ ……))rr + + dd11
RR = = dd00
Divide S by rS = quotientdi = remainder
i = 0
S = D
Start
i = m – 1
i = i + 1
Done
no
yes
9Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Conversion of Decimal NumbersConversion of Decimal NumbersExamplesExamples
Problem: Convert (a) 179 to binary, (b) 467 to octal, and (c) 3417 to hexadecimal.
Solution:
Therefore,Therefore,101100111011001122==1791791010
remainder 1 (MSD)remainder 1 (MSD)00==22÷÷11
5511224499
1122
11224488
==
====
========
remainder 1remainder 1÷÷ 221111remainder 1remainder 122÷÷55
remainder 0remainder 022÷÷2222remainder 0remainder 022÷÷4444
22
9999
8877
÷÷
÷÷÷÷
remainder 0remainder 022
remainder 1remainder 122remainder 1 (LSD)remainder 1 (LSD)2211
Therefore,Therefore,72372388==4674671010
88
007755
======
remainder 7 (MSD)remainder 7 (MSD)88÷÷778877
5566
÷÷÷÷
remainder 2remainder 288remainder 3 (LSD)remainder 3 (LSD)8844
Therefore,Therefore,D59D591616==341734171010
88
007755
======
remainder 13 (MSD)remainder 13 (MSD)1616÷÷33111111
2244
÷÷÷÷
3377
remainder 5remainder 51616remainder 9 (LSD)remainder 9 (LSD)161633
(a)(a)(b)(b)
(c)(c)
10Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Addition of Binary NumbersAddition of Binary Numbers
ci+1, si = xi + yi + ci
c0 = 0
i = 0
Start
i = m
i = i + 1
Done
no
yes
11110000
xi
11001100
yi
10101010
ci sici+1
0010
110101100110
s7
0
1
1
128
s8
0
1
1
256
s9
0
1
1
512
s10
1
1
s4
1
1
1
1
16
s5
0
1
1
0
32
s6
1
1
1
1
64
x + y (1110)
Carries
y (123)
x (987)
s0s1s2s3
0110
110
1101
1101
8 4 12
0111x + y
111Carries
321y
789x
Addition of Binary DigitsDecimal Addition
Binary Addition
11Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
11110000xi
11001100yi
10101010
ci dibi+1
0011
110000100111
Subtraction of Binary NumbersSubtraction of Binary Numbers
bi+1, di = xi – yi – bi
b0 = 0
i = 0
Start
i = m
i = i + 1
Done
no
yes
d7
0
1
1
128
d8
1
0
1
256
d9
1
0
1
512
d4
0
0
1
1
16
d5
1
0
1
0
32
d6
1
1
1
1
64
x – y (864)
Borrows
y (123)
x (987)
d0d1d2d3
0000
000
1101
1101
8 4 12
468x – y
000Barrows
321y
789x
Subtraction of Binary DigitsDecimal Subtraction
Binary Subtraction
Back to TwoBack to Two’’ss--complement Subtractioncomplement Subtraction
12Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Sign Magnitude RepresentationSign Magnitude RepresentationStart
additionStart
subtraction
s2 = s′2
s1 = s2
mr = m1 + m2sr = s1
m1 > m2
m1 = m2
mr = m1 – m2sr = s1
mr = 0sr = 0
mr = m2 – m1sr = s2
Done
yesno
yesno
yesno
A sign magnitude number <s, m>, consists of two parts:
Sign and MagnitudeSign and MagnitudeThe sign is either + or –The magnitude is an integer between 0 and the largest representable value
Examples:011110110111101122 = +123= +1231010111110111111101122 = = ––1231231010
D1 = < s1, m1 > and D2 = < s2, m2 > yields the result Dr = < sr, mr >
13Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Radix-complement of a number DD == is equal to:
If digit complement dd′′ = = ((r r –– 1) 1) –– dd then
Therefore,is a negative number of D D , since
Complement Number SystemComplement Number System
∑−
=
⋅1
0
m
i
ii rd∑
−
=
⋅1
0
m
i
ii rd
1))1(( +−−=−= DrDrD mm 1))1(( +−−=−= DrDrD mm
∑−
=
1
0'
m
iid∑
−
=
1
0'
m
iid
((rrmm –– 1)1) –– DD = ( (= ( (rr –– 1) (1) (rr –– 1) 1) …… ((rr –– 1) 1) –– ((ddm m –– 11 ddm m –– 2 2 …… dd0 0 ) )) )= ( (= ( (rr –– 1) 1) –– ddm m –– 11)) ( (( (rr –– 1) 1) –– ddm m –– 2 2 ) ) …… ( (( (rr –– 1) 1) –– dd0 0 ))
= = dd′′mm –– 1 1 dd′′mm –– 2 2 …… dd′′00 = = = D= D′′
1'+= DD 1'+= DD
DD 0=+ DD 0=+ DD
14Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Complement Number SystemComplement Number System
60--971--8820-7931-6A42-5B53-4
FEDCBA
3210
Digit
2---3---4---5---
C64-D75-E860F971
0---1---
Binary Octal Hexa-decimalDecimal
011101117
011001106
010101015
010001004
001100113
001000102
000100011
1000 or 000000000
10011111-1
10101110-2
10111101-3
11001100-4
11011011-5
11101010-6
11111001-7
-1000-8
Sign-Magnitude
Two’s ComplementDecimal
Digit Complements Two’s Complement and Sign-Magnitude Representations
15Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
TwoTwo’’ss--complement Additioncomplement Addition
Adding two positive numbers generates a correct result
Adding two negative numbers generates a correct result if carry is ignored
000000
(+6)(+6)111100(+4)(+4)001100++(+2)(+2)110000
Ignored carry = Ignored carry = 11 000000
((––6)6)110011((––4)4)001111++((––2)2)111111
16Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
TwoTwo’’ss--complement Additioncomplement AdditionAdding a positive and a negative number generates a correct result
Adding large numbers may generate an incorrect result because of an overflow
000000
((––2)2)111111((––4)4)001111++(+2)(+2)110000
111100
((––7)7)000011(+5)(+5)001100++(+4)(+4)001100
Ignored carry = Ignored carry = 11 111100
(+7)(+7)111100((––5)5)110011++((––4)4)001111
Overflow rule: an overflow occurs when the sign of the sum is different than the signs of both operands
17Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Subtraction using two’s complementDirect subtraction using subtraction
standard procedure
TwoTwo’’ss--complement Subtractioncomplement Subtraction
Startaddition
Startsubtraction
B2 = B2‘ + 1
Br = B1 + B2
Done
–
1
0
0
1
011(–2) ignore barrow =1
011Barrows
010(+4)
000(+2)
+
1
0
0
1
011(–2)
00Carries
011Two’s complement of (+4)
000(+2)
+
0
0
0
0
010(+2) ignore carry =1
00Carries
001Two’s complement of (–8)
011(–4)
18Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Decimal Multiplication
Binary Multiplication
Binary MultiplicationBinary Multiplication
××
productproduct22881111 ×× multiplicandmultiplicand441133 ×× multiplicandmultiplicand2244multipliermultiplier3311multiplicandmultiplicand4411
0011111100000000
11 111111
000011
1111
11
00
1111
11
0011
××
product product (182)(182)001100111111
multiplier multiplier (13)(13)11multiplicand multiplicand (14)(14)00
19Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
ShiftShift--andand--add Multiplicationadd MultiplicationStart
PP = 0i = 0
bi = 1
Product = PP
Done
yesno
PP = PP + (2i × MD)
i = i + 1
i = m
MR = multiplier bm–1 …b1 b0 (m-bit)MD = multiplicand (n-bit)PP = partial product
Example of shift-and-add multiplication
shifted multiplicandshifted multiplicand00111111fourth partial productfourth partial product00111100000011
shifted zerosshifted zeros00000000
first partial productfirst partial product00000000shifted multiplicandshifted multiplicand00111111second partial productsecond partial product00111111
11 1111
00
0000
11
1111
11
11
1111
11
11
0011
××
product product (182)(182)001100shifted multiplicandshifted multiplicand1111
third partial productthird partial product00
multiplier multiplier (13)(13)11multiplicand multiplicand (14)(14)00
no
yes
20Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
TwoTwo’’ss--complement Multiplicationcomplement MultiplicationUse multiplication procedure for unsigned numbers
Negate multiplicand if multiplier sign is negative
ignore carryignore carry
extended partial productextended partial product00111100110011111111extended, shifted, and negated extended, shifted, and negated muliplicandmuliplicand
001111110000
11
00110011
00
001100111111
all zerosall zeros00000000extended partial productextended partial product00111100110011
extended shifted multiplicandextended shifted multiplicand0011000011
extended partial productextended partial product000000000000extended multiplicandextended multiplicand001100001111extended partial productextended partial product001100001111
00 11
00
11
1111
00
00
00
0000
11
11
0000
11
11
1111
××
product product (182)(182)001100
all zerosall zeros0000
extended partial productextended partial product0000
multiplier multiplier ((––13)13)11multiplicand multiplicand ((––14)14)00
Note:•Carry out of MSB in the third PP is ignored before sign extension
•Carry out of MSB is ignored in the final product
•Sign truncation is needed in the final product
21Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Decimal Division
Binary Division
Binary DivisionBinary Division
shifted shifted ((divisor x divisor x 3)3)2244
)1414
remainderremainder44
reduced dividendreduced dividend6644shifted shifted ((divisor x divisor x 11))4411dividenddividend668811quotientquotient3311
11110011
110000001111001111
11
1111001111
11001111
reduced dividendreduced dividend001100shifted divisorshifted divisor000000
shifted divisorshifted divisor00reduced dividendreduced dividend001100
shifted divisorshifted divisor001111
)11101110
remainder remainder (14)(14)000011
reduced dividendreduced dividend001100
shifted divisorshifted divisordividend dividend (186)(186)001100quotient quotient (13)(13)110011
22Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Floating-point numbers have the formmantissamantissa ×× ((radixradix) ) exponentexponent
Since radix is implicit, only mantissa and exponent must be represented explicitlyFloating-point numbers are fixed-point numbers given by the mantissa, whose radix point is specified by the exponentExponent is represented in the excess-code format called characteristic, obtained by adding a bias to the exponent:
bias =bias = radixradixss –– 11where ss is equal to the number of bits in the exponent field
FloatingFloating--point Numberspoint Numbers
2121
Mantissamagnitude
Signed exponent
Mantissasign
NormalizedFraction
Excess-127 characteristicSign
NormalizedFraction
Excess-1032 characteristicSign
32-bit standard
0 1 9 31
0 1 12 63
Implied binary point
Implied binary point64-bit standard
General format
23Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
FixedFixed--point vs. Floatingpoint vs. Floating--pointpoint
ExponentMantissaInteger
11 numbersbetween 1000 and 2000
1001 numbersbetween 1000 and 2000Example
~ 1×~ 100×Precision
~ 10101~ 104Range
0 – 99 × 100 – 9999Representable numbers
4-digit floating-point number4-digit fixed number
The range is the interval of numbers from the largest to the smallest representable numberThe precision is the amount of numbers in a number interval
24Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Binary Codes for Decimal NumbersBinary Codes for Decimal Numbers
Binary-coded Decimals (BCD)
2421 Code
Excess-3 Code
Biquinary Code
9
8
7
6
5
4
3
2
1
0
Decimal digit
1000100101011010111
1000010100111000110
1000001100010110101
0110000011101000100
0101000011000110011
0100100010100100010
0100010010000010001
0100001001100000000
1010000110011111001
1001000101111101000
BCD (8421) 2421 BiquinaryExcess-3
25Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Character CodesCharacter Codesb6b5b4
O
N
M
L
K
J
I
H
G
F
E
D
C
B
A
@
100
_
^
]
\
[
Z
Y
X
W
V
U
T
S
R
Q
P
101
o
n
m
l
k
j
i
h
g
f
e
d
c
b
a
‘
110
?
>
=
<
;
:
9
8
7
6
5
4
3
2
1
0
011
z*SUBLF1010
{+ESCVT1011
|,FSFF1100
}-GSCR1101
~.RSSO1110
y)EMHT1001
1111
1000
0111
0110
0101
0100
0011
0010
0001
0000
b3b2b1b0
w’ETBBEL
v&SYNACK
u%NAKENQ
t$DC4EOT
s#DC3ETX
r“DC2STX
q!DC1SOH
pSPDLENUL
DEL/USSI
x(CANBS
000 001 111010
American Standard Code for Information Interchange (ASCII)
NUL NullSOH Start of headingSTX Start of textETX End of textEOT End of transmissionENQ EnquiryACK AcknowledgeBEL BellBS BackspaceHT Horizontal tabLF Line feedVT Vertical tabFF Form feedCR Carriage returnSO Shift outSI Shift inSP SpaceDLE Data link escapeDC1 Device control 1DC2 Device control 2DC3 Device control 3DC4 Device control 4NAK Negative acknowledgementSYN SynchronizeETB End transmission blockCAN CancelEM End of mediumSUB SubstituteESC EscapeFS File separatorGS Group separatorRS Record separatorUS Unit separatorDEL Delete or rubout
26Copyright © 2004-2005 by Daniel D. Gajski Slides by Philip Pham, University of California, Irvine
Any n-bit string can be represented by an n-cube with 2n vertices, each corresponding to a particular string of n bits
Any m-subcube has the samen – m bits and m bits that take all 2n possible combinations of 0s and 1s
Distance between two vertices is equal to 1 + the number of vertices on the shortest path between two vertices
Cube RepresentationCube Representation0 1
10 11
00 01
101 111
001 011
000 010
100 110
1110 1111
0000 0001
1010 0111
1000 1001
1100 1101
0110 0111
0010 0011
0100 0101
(a)
(b)
(c)
(d)
n-cubes for n = 1, 2, 3, and 4