Upload
kelley-jacobs
View
232
Download
0
Embed Size (px)
Citation preview
Digital Design:Principles and Practices
Chapter 2Number Systems and Codes
2.1 Positional Number Systems
3
Counting in Binary
With n bits, you can count from 0 to 2n – 1
For example, with four bits (n=4), you can count from 0 to 15:
24 – 1 = 16 – 1 = 15
4
Figure 2–1 Illustration of a simple binary counting application.
Counting in Binary – An Application
5
Positional Number System
• The traditional number system that we learned in school and use everyday in business.
• A number is represented by A string of digits Each digit position has an associated weight
• Example #1: 1734 = 1*1000 + 7*100 + 3*10 + 4*1 Each weight is a power of 10 corresponding to the digit’s position.
• Example #2: 5185.68 = 5*1000 + 1*100 + 8*10 + 5*1 + 6*0.1 + 8*0.01
6
Positional Number System
• In general, a number D of the form d1d0.d-1d-2 has the value:
D = d1*101 + d0*100 + d-1*10-1 + d-2*10-2
10 is called the base or radix of the number system.
radix point ( 小數點 )
• in decimal number system: decimal point 十進位的小數點
• in binary number system: binary point 二進位的小數點
7
Positional Number System
• Radix = r• General form:
• There are p digits to the left of the radix point and n to the right.
• The value:
1 2 1 0 1 2. p p nd d d d d d d
1pi
ii n
D d r
8
Positional Number System - Binary
• Radix = r = 2• General form:
• The value:
1 2 1 0 1 2. p p nb b b b b b b
1
2p
ii
i n
B b
9
Positional Number System
• Digit Binary digit (bit)
• Radix point Decimal point, Binary point
• Most Significant Bit (MSB)• Least Significant Bit (LSB)
• When dealing with binary and other non-decimal numbers, we use a subscript to indicate the radix of each number.
10
Binary to Decimal Conversion
• 100112 = 1910
100112 = 1*16 + 0*8 + 0*4 + 1*2 + 1*1 = 1910
• 1000102 = 3410
• 101.0012 = 5.12510
2.2 Octal and HexadecimalNumbers
12
Octal & Hexadecimal
• The octal number system uses radix 8. 8 digits (0-7)
• The hexadecimal number system uses radix 16. 16 digits (0-9 and then A-F)
13
Binary, Decimal, Octal, & Hexadecimal
Table 2-1
14
Binary-to-Octal Conversion
• Binary-to-Octal Conversion Method (no binary point): Starting at the binary point and working left, we simply separate
the bits into groups of three and replace each group with the corresponding octal digit.
• Example #1: 1000110011102 = 100 011 001 1102 = 43168
• Example #2: 111011011101010012 = 011 101 101 110 101 0012
= 3556518
15
Binary-to-Hexadecimal Conversion
• Binary-to-Hex Conversion Method (no binary point): Starting at the binary point and working left, we simply separate
the bits into groups of four bits and replace each group with the corresponding hexadecimal digit.
• Example #1: 1000110011102 = 1000 1100 11102 = 8CE8
• Example #2: 111011011101010012 = 0001 1101 1011 1010 10012
= 1DBA98
16
Binary-to-Octal Conversion &Binary-to-Hexadecimal Conversion
• Example: 10.10110010112 = 010 . 101 100 101 1002 = 2.54548
= 0010 . 1011 0010 11002 = 2.B2C16
17
Octal-to-Binary Conversion &Hexadecimal-to-Binary Conversion
• Converting from octal or hexadecimal to binary is very easy. We simply replace each octal or hexadecimal digit with the corresponding 3- or 4-bit string.
• Examples: 13578 = 001 011 101 1112
2046.178 = 010 000 100 110 . 001 1112
BEAD16 = 1011 1110 1010 11012
9F.46C16 = 1001 1111 . 0100 0110 11002
18
Hexadecimal Numbers
• 1 byte = 8 bits• 1 hexadecimal digital = 1 nibble = 4 bits• 8 nibbles = 32 bits = 4 bytes
• Hexadecimal numbers are often used to describe a computer’s memory address space.
• Many computer programming languages (e.g. C/C++) use the prefix “0x” to denote a hexadecimal number, for example, 0xBFC0000.
2.3 General Positional-Number-System Conversions
20
Number System Conversions• In general, conversion between two radices cannot be done by
simple substitutions; arithmetic operations are required.
• In this section, we show how to convert a number in any radix to radix 10 (decimal) and vice versa.
• Radix r to radix 10 (decimal) conversion:
r: the radix of the number which is to be converted p: there are p digits to the left of the radix point n: there are n digits to the right of the radix point
1pi
ii n
D d r
21
Radix r to Radix 10 ConversionExamples
• 1CE816 = 1*163 + 12*162 + 14*161 + 8*160 = 740010
• F1A316 = 15*163 + 1*162 + 10*161 + 3*160 = 6185910
• 436.58 = 4*82 + 3*81 + 6*80 + 5*8-1 = 286.62510
• 132.34 = 1*42 + 3*41 + 2*40 + 3*4-1 = 30.7510
• An alternative method:F1A316 = ( ( (15)*16 + 1)*16 + 10 )*16 + 3 = 6185910
22
Binary, Decimal, Octal, & Hexadecimal
Table 2-1
23
Radix r to Radix 10 Conversion- An Alternative Method
• General Form:
• Example: F1A316 = ( ( (15)*16 + 1)*16 + 10 )*16 + 3 = 6185910
• The method can be used to convert a decimal number D to a radix r.
1 2 1 0(( (( ) ) ) )p pD d r d r r d r d
24
Radix 10 to Radix r Conversion
• General Form:
• If we divide D by r, then:Quotient:
Remainder: d0
• Successive divisions by r yield successive digits of D from right to left, until all the digits of D have been derived.
1 2 1 0(( (( ) ) ) )p pD d r d r r d r d
1 2 1( (( ) ) )p pQ d r d r r d
25
Radix 10 to Radix r ConversionAn Example (r=16)
61859 ÷ 16 = 3866 …… 3 (LSB)
÷ 16 = 241 …… 10
÷ 16 = 15 …… 1
÷ 16 = 0 …… 15
(MSB)
6185910 = F1A316
26
Radix 10 to Radix r ConversionAn Example (r=2)
179 ÷ 2 = 89 …… 1 (LSB)
÷ 2 = 44 …… 1
÷ 2 = 22 …… 0
÷ 2 = 11 …… 0
÷ 2 = 5 …… 1
÷ 2 = 2 …… 1
÷ 2 = 1 …… 0
÷ 2 = 0 …… 1
(MSB)
17910 = 101100112
27
Conversion Methods for Common Radices (I)Table 2-1
28
Conversion Methods for Common Radices (II)
Table 2-1 (cont’d)
EXAMPLE 2-26Convert the following hexadecimal numbers to decimal.(a)1C16
(b)A8516
Hexadecimal-to-Decimal Conversion
Ans: (a) 2810 (b) 269310
Decimal-to-Hexadecimal Conversion
EXAMPLE 2-28Convert the decimal number 650 to hexadecimal.
Ans: 28A16
Octal-to-Decimal Conversion
Example: (2374)8 = ( )10
Ans: 127610
Decimal-to-Octal Conversion
Example: (359)10 = ( )8
Ans: 5478
Octal-to-Binary Conversion
EXAMPLE 2-31Convert each of the following octal numbers to binary:(a)138
(b)258
(c)1408
(d)75268
Ans: (a) 001011 (b) 010101 (c) 001100000 (d) 111101010110
Binary-to-Octal Conversion
EXAMPLE 2-32Convert each of the following binary numbers to octal:(a)110101(b)101111001(c)100110011010(d)11010000100
Ans: (a) 658 (b) 5718 (c) 46328 (d) 32048
2.4 Addition and Subtraction ofNon-decimal Numbers
36
Addition & Subtraction of Non-decimal Numbers
• Addition and subtraction of non-decimal numbers by hand uses the same technique that we learned in elementary school for decimal numbers.
• Binary Addition s: sum cin: input carry cout: output carry
• Binary Subtraction (minuend minus subtrahend yields difference) d: difference bin: input borrow bout: output borrow
37
Binary Addition & Subtraction Table
Table 2-3 Binary addition and subtraction table
38
Binary Addition
39
Binary Addition• Binary Addition
s: sum cin: input carry cout: output carry
C 1 0 1 1 1 1 0 0 0
X 190 1 0 1 1 1 1 1 0
Y + 141 + 1 0 0 0 1 1 0 1
X + Y 331 1 0 1 0 0 1 0 1 1
40
Binary Subtraction
41
Binary Subtraction (cont’d)
• Binary Subtraction (minuend minus subtrahend yields difference) d: difference bin: input borrow bout: output borrow
42
Binary Subtraction• Binary Subtraction
d: difference bin: input borrow bout: output borrow
B 0 1 1 0 1 1 0 1 0
X 210 1 1 0 1 0 0 1 0
Y - 109 - 0 1 1 0 1 1 0 1
X - Y 101 0 1 1 0 0 1 0 1
43
Binary Addition & Subtraction Table
Table 2-3 Binary addition and subtraction table
44
Hexadecimal AdditionC 1 1 0 0
X 1 9 B 916
Y + C 7 E 616
X+Y E 1 9 F16
C 1 1 0 0
X 1 9 11 9
Y + 12 7 14 6
X+Y 14 17 25 F
14 16+1 16+9 15
E 1 9 F
2.5 Representation of Negative Numbers
2.5.1 Signed-Magnitude Representation
47
Signed-Magnitude Representation
• Definition of the Sign Bit: The left-most bit (a.k.a. MSB) in a signed binary number is the sign bit, which tells you whether the number is positive or negativeA 0 sign bit indicates a positive numberA 1 sign bit indicates a negative number
• Two possible representations of zero: “+0” and “–0”
• An n-bit signed-magnitude integer lies within the range –(2n–1–1) through +(2n–1–1)
48
Signed-Magnitude RepresentationExamples
• 010101012 = +8510
• 110101012 = –8510
• 011111112 = +12710
• 111111112 = –12710
• 000000002 = +010
• 100000002 = –010
49
Signed-Magnitude RepresentationAddition & Subtraction
• Now suppose that we wanted to build a digital logic circuit that adds signed-magnitude numbers.
• If the signs are the same, it must add the magnitudes and give the result the same sign.
• If the signs are different, it must compare the magnitudes, subtract the smaller from the larger, and give the result the sign of the larger.
• Adders for complement number systems are much simpler.
2.5.2 Complement Number Systems2.5.3 Radix-Complement Representation2.5.4 Two’s-Complement Representation
2.5.5 Diminished Radix-Complement Representation
2.5.6 One’s-Complement Representation2.5.7 Excess Representations
Vocabulary
• Complement (n.) 補充、補充物、補數• Compliment (n.) (v.) 讚美
• 兩個字發音相同
1’s Complement• How do you calculate the 1’s complement of a binary
number?Ans: Change all 1s to 0s and all 0s to 1s
• For example:The 1’s complement of the binary number 10110010 is 01001101.
1 0 1 1 0 0 1 0 Binary Number↓ ↓ ↓ ↓ ↓ ↓ ↓ ↓0 1 0 0 1 1 0 1 1’s Complement
Figure 2–2 Example of inverters used to obtain the 1’s complement of a binary number.
Thomas L. FloydDigital Fundamentals, 9e
Copyright ©2006 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458
All rights reserved.
1’s Complement (cont’d)
2’s Complement• How do you calculate the 2’s complement of a binary number?
Ans: (2’s complement of a binary number) = (1’s complement of the binary number) + 1
• For example:The 2’s complement of the binary number 10110010 is 01001110.
10110010 Binary Number01001101 1’s complement
+ 1 Add 101001110 2’s complement
Figure 2–3 Example of obtaining the 2’s complement of a negative binary number.
Thomas L. FloydDigital Fundamentals, 9e
Copyright ©2006 by Pearson Education, Inc.Upper Saddle River, New Jersey 07458
All rights reserved.
2’s Complement (cont’d)
Converting 1’s or 2’s Complements Back to the Original Binary Numbers
• By performing 1’s or 2’s complement conversion again, you can get the original binary number!
Signed-Magnitude Representation
• For example:
(+25)10 00011001
(-25)10 10011001
0 0 0 1 1 0 0 1↑ ↑
Sign bit Magnitude bits
Signed-Magnitude Representation
• The Sign-Magnitude Representation has two binary numbers for (0)10
(+0)10 00000000
(-0)10 10000000
1’s Complement Representation
• For example:
(+25)10 00011001 (the same as the Signed-Magnitude Rep.)
(-25)10 11100110 (1’s complement of (+25)10)
• The 1’s Complement Rep. has two binary numbers for (0)10
(+0)10 00000000
(-0)10 11111111
2’s Complement Representation
• For example:
(+25)10 00011001 (the same as the Signed-Magnitude Rep.)
(-25)10 11100111 (2’s complement of (+25)10)
• The 2’s Complement Rep. has only one binary numbers for (0)10
(+0)10 00000000
(-1)10 11111111
(-128)10 10000000
61
Decimal and 4-bit Numbers
62
Ranges of Different Number System Representations
• m-bit number
• Two’s Complement Representation –(2m–1) to +(2m–1 – 1)
• One’s Complement Representation –(2m–1 – 1) to +(2m–1 – 1)
• Signed-Magnitude Representation –(2m–1 – 1) to +(2m–1 – 1)
• Excess-2m–1 Representation –(2m–1) to +(2m–1 – 1)
63
Excess-2m-1 Representations
• In excess-B representation, an m-bit string whose unsigned integer value is M represents the signed integer M – B, where B is called the bias of the number system.
• In excess-2m-1 representations, B equals 2m-1.
• The range of the representation is exactly the same as that of m-bit two’s-complement numbers. In fact, the representations of any number in two systems are identical except for the sign bits, which are always opposite (assume that B = 2m-1).
2.6 Two’s-Complement Additionand Subtraction
65
2’s-Complement Addition and Subtraction
Figure 2-3 A modular counting representation of 4-bit two’s-complement numbers.
66
2’s-Complement Addition
+3 0011+ +4 + 0100
+7 0111
–2 1110
+ –6 + 1010
–8 11000
+6 0110+ –3 + 1101
+3 10011
+4 0100+ –7 + 1001
–3 1101
67
2’s-Complement Addition - Overflow
–3 1101
+ –6 + 1010
–9 10111
–8 1000
+ –8 + 1000
–16 10000
+5 0101+ +6 + 0110
+11 1011
+7 0111+ +7 + 0111
+14 1110
= +7
= +0
= -5
= -2
68
• If an addition operation produces a result that exceeds the range of the number, overflow is said to occur.
• A simple rule for detecting overflow in addition: An addition overflows if the addends’ signs are the
same but the sum’s sign is different from the addends’.
2’s-Complement Addition - Overflow
69
2’s-Complement Subtraction
• minuend – subtrahend = difference ( 被減數 ) – ( 減數 ) = ( 差 ) For example: 9 – 6 = 3
• 2’s-Complement Subtraction Procedures: Negate the subtrahend by taking its 2’s complement Add the negated subtrahend to the minuend using
normal rules for addition
70
2’s-Complement Subtraction
• Negating the subtrahend and adding the minuend can be accomplished with only one addition operation as follows: Perform a bit-by-bit complement of the subtrahend and
add the complemented subtrahend to the minuend with an initial carry (Cin) of 1 instead of 0.
See examples.
71
2’s-Complement SubtractionAn Example
1 Cin
+4 0100 0100– +3 – 0011 + 1100
+1 1 0001
72
2’s-Complement SubtractionAn Example
1 Cin
+3 0011 0011– –4 – 1100 + 0011
+7 0111
73
1 Cin
–5 1011 1011– 4 – 0100 + 1011
–9 1 0111
2’s-Complement Subtraction - Overflow
• Overflow in subtraction can be detected by examining the signs of the minuend and the complemented subtrahend, using the same rule as in addition.
• Example:
EXAMPLE 2-14
Express the decimal number -39 as an 8-bit binary number in the sign-magnitude, 1’s complement, and 2’s complement forms.
Determine the decimal value of this signed binary number expressed in sign-magnitude representation: 10010101.
EXAMPLE 2-15( Signed-Magnitude Rep. Decimal Value )
EXAMPLE 2-16
Determine the decimal values of the signed binary numbers expressed in 1’s complement representation:(a) 00010111(b) 11101000
( 1’s Complement Rep. Decimal Value )
EXAMPLE 2-17
Determine the decimal values of the signed binary numbers expressed in 2’s complement representation:(a) 01010110(b) 10101010
( 2’s Complement Rep. Decimal Value )
Numbers Are Added Two at a Time
EXAMPLE 2-19Add the signed numbers: 01000100, 00011011, 00001110, and 00010010.
Arithmetic Operations with Signed Numbers - Subtraction
Related Problem for EXAMPLE 2-20:
Subtract 01000111 from 01011000.
2.10 Binary Codes forDecimal Numbers
81
Binary Codes for Decimal Numbers
• Code: A set of n-bit strings in which different bit strings represent different numbers or other things.
• Code word: A particular combination of n bit-values.
• A code that uses n-bit strings need not contain 2n valid code words.
• At least 4 bits are needed to represent the ten decimal digits.
• Binary-coded decimal (BCD)
82
Decimal Codes
83
Decimal Codes• BCD = 8421 code• 2421 code
Self complementing: The code word for the 9s’ complement of any digit may be obtained by complementing the individual bits of the digit’s code word.
• Biquinary quinary: 5 The first two bits in a code word indicate whether the number is in the
range 0-4 or 5-9. The last five bits indicate which of the five numbers in the selected
range is represented Error detection: can detect one bit error
• 1-out-fo-10 code Uses 10 out of 1024 possible 10-bit code words
Decimal BCD
EXAMPLE 2-33Convert each of the following decimal numbers to BCD:(a)35(b)98(c)170(d)2469
BCD Decimal
EXAMPLE 2-34Convert each of the following BCD codes to decimal:(a)10000110(b)001101010001(c)1001010001110000
BCD Addition
4 個 bits 一組,每組超過 9 (10012) 要加 6 (01002)
BCD Addition
EXAMPLE 2-35Add the following BCD numbers:(a)0011 + 0100(b)00100011 + 00010101(c)10000110 + 00010011(d)010001010000 + 010000010111
BCD Addition
EXAMPLE 2-36Add the following BCD numbers:(a)1001 + 0100(b)1001 + 1001(c)00010110 + 00010101(d)01100111 + 01010011
2.11 Gray Code
90
A Mechanical Encoding DiskUsing a 3-bit Binary Code
91
A Mechanical Encoding DiskUsing a 3-bit Gray Code
92
3-bit Binary Code & Gray Code
93
Constructing a Gray Code
• A 1-bit Gray code has two code words, 0 and 1.
• The first 2n code words of an (n+1)-bit Gray code equal the code words of an n-bit Gray code, written in order with a leading 0 appended.
• The last 2n code words of an (n+1)-bit Gray code equal the code words of an n-bit Gray code, but written in reverse order with a leading 1 appended.
94
Constructing a Gray Code
1-bit Gray Code 2-bit Gray Code 3-bit Gray Code01
00011110
000001011010110111101100
95
Binary code word Gray-code code word
• The bits of an n-bit binary or Gray-code code word are numbered from right to left, from 0 to n – 1.
• Bit i of a Gray-code code word is 0 if bits i and i + 1 of the corresponding binary code word are the same, else bit i is 1. (When i + 1 = n, bit n of the binary code word is considered to be 0.)
96
Binary Gray Code[Floyd]
• Step 1: The most significant bit (left-most) in the Gray code is the same as the corresponding MSB in the binary number.
• Step 2:Going from left to right, add each adjacent pair of binary code bits to get the next Gray code bit. Discard carries.
97
Gray Code Binary[Floyd]
• Step 1:The most significant bit (left-most) in the binary code is the same as the corresponding bit in the Gray code.
• Step 2:Add each binary code bit generated to the Gray code bit in the next adjacent position. Discard carries.
2.12 Character Codes
99
ASCII
• American Standard Code for Information Interchange
• Each character is represented by a 7-bit string
• 27 = 128 (total number of different characters)
• “Yeccch!”1011001 1100101 1100011 1100011 1100011 1101000
0100001
100
ASCII
101
ASCII (cont’d)
2.13 Codes for Actions, Conditions, and States
103
States in a Traffic-Light Controller
104
Control Structure for a Digital System