Download presentation
Presentation is loading. Please wait.
Published byKelley Jacobs Modified over 9 years ago
1
Digital Design: Principles and Practices Chapter 2 Number Systems and Codes
2
2.1 Positional Number Systems
3
Counting in Binary With n bits, you can count from 0 to 2 n – 1 For example, with four bits (n=4), you can count from 0 to 15: 2 4 – 1 = 16 – 1 = 15 3
4
Figure 2–1 Illustration of a simple binary counting application. Counting in Binary – An Application 4
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 5
6
Positional Number System In general, a number D of the form d 1 d 0.d -1 d -2 has the value: D = d 1 *10 1 + d 0 *10 0 + 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 二進位的小數點 6
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: 7
8
Positional Number System - Binary Radix = r = 2 General form: The value: 8
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. 9
10
Binary to Decimal Conversion 10011 2 = 19 10 10011 2 = 1*16 + 0*8 + 0*4 + 1*2 + 1*1 = 19 10 100010 2 = 34 10 101.001 2 = 5.125 10 10
11
2.2 Octal and Hexadecimal Numbers
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) 12
13
Binary, Decimal, Octal, & Hexadecimal Table 2-1 13
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: 100011001110 2 = 100 011 001 110 2 = 4316 8 Example #2: 11101101110101001 2 = 011 101 101 110 101 001 2 = 355651 8 14
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: 100011001110 2 = 1000 1100 1110 2 = 8CE 8 Example #2: 11101101110101001 2 = 0001 1101 1011 1010 1001 2 = 1DBA9 8 15
16
Binary-to-Octal Conversion & Binary-to-Hexadecimal Conversion Example: 10.1011001011 2 = 010. 101 100 101 100 2 = 2.5454 8 = 0010. 1011 0010 1100 2 = 2.B2C 16 16
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: 1357 8 = 001 011 101 111 2 2046.17 8 = 010 000 100 110. 001 111 2 BEAD 16 = 1011 1110 1010 1101 2 9F.46C 16 = 1001 1111. 0100 0110 1100 2 17
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. 18
19
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 20
21
Radix r to Radix 10 Conversion Examples 1CE8 16 = 1*16 3 + 12*16 2 + 14*16 1 + 8*16 0 = 7400 10 F1A3 16 = 15*16 3 + 1*16 2 + 10*16 1 + 3*16 0 = 61859 10 436.5 8 = 4*8 2 + 3*8 1 + 6*8 0 + 5*8 -1 = 286.625 10 132.3 4 = 1*4 2 + 3*4 1 + 2*4 0 + 3*4 -1 = 30.75 10 An alternative method: F1A3 16 = ( ( (15)*16 + 1)*16 + 10 )*16 + 3 = 61859 10 21
22
Binary, Decimal, Octal, & Hexadecimal Table 2-1 22
23
Radix r to Radix 10 Conversion - An Alternative Method General Form: Example: F1A3 16 = ( ( (15)*16 + 1)*16 + 10 )*16 + 3 = 61859 10 The method can be used to convert a decimal number D to a radix r. 23
24
Radix 10 to Radix r Conversion General Form: If we divide D by r, then: Quotient: Remainder: d 0 Successive divisions by r yield successive digits of D from right to left, until all the digits of D have been derived. 24
25
Radix 10 to Radix r Conversion An Example ( r =16) 61859 ÷ 16 = 3866 …… 3 (LSB) ÷ 16 = 241 …… 10 ÷ 16 = 15 …… 1 ÷ 16 = 0 …… 15 (MSB) 61859 10 = F1A3 16 25
26
Radix 10 to Radix r Conversion An 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) 179 10 = 10110011 2 26
27
Conversion Methods for Common Radices (I) 27 Table 2-1
28
Conversion Methods for Common Radices (II) 28 Table 2-1 (cont’d)
29
EXAMPLE 2-26 Convert the following hexadecimal numbers to decimal. (a)1C 16 (b)A85 16 Hexadecimal-to-Decimal Conversion Ans: (a) 28 10 (b) 2693 10
30
Decimal-to-Hexadecimal Conversion EXAMPLE 2-28 Convert the decimal number 650 to hexadecimal. Ans: 28A 16
31
Octal-to-Decimal Conversion Example: (2374) 8 = ( ) 10 Ans: 1276 10
32
Decimal-to-Octal Conversion Example: (359) 10 = ( ) 8 Ans: 547 8
33
Octal-to-Binary Conversion EXAMPLE 2-31 Convert each of the following octal numbers to binary: (a)13 8 (b)25 8 (c)140 8 (d)7526 8 Ans: (a) 001011 (b) 010101 (c) 001100000 (d) 111101010110
34
Binary-to-Octal Conversion EXAMPLE 2-32 Convert each of the following binary numbers to octal: (a)110101 (b)101111001 (c)100110011010 (d)11010000100 Ans: (a) 65 8 (b) 571 8 (c) 4632 8 (d) 3204 8
35
2.4 Addition and Subtraction of Non-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 c in : input carry c out : output carry Binary Subtraction (minuend minus subtrahend yields difference) d : difference b in : input borrow b out : output borrow 36
37
Binary Addition & Subtraction Table 37 Table 2-3 Binary addition and subtraction table
38
Binary Addition 38
39
Binary Addition 39 Binary Addition s : sum c in : input carry c out : output carry C1 0 1 1 1 1 0 0 0 X1901 0 1 1 1 1 1 0 Y+ 141+1 0 0 0 1 1 0 1 X + Y3311 0 1 0 0 1 0 1 1
40
Binary Subtraction 40
41
Binary Subtraction (cont’d) 41 Binary Subtraction (minuend minus subtrahend yields difference) d : difference b in : input borrow b out : output borrow
42
Binary Subtraction 42 Binary Subtraction d : difference b in : input borrow b out : output borrow B0 1 1 0 1 1 0 1 0 X2101 1 0 1 0 0 1 0 Y- 109-0 1 1 0 1 1 0 1 X - Y101 0 1 1 0 0 1 0 1
43
Binary Addition & Subtraction Table 43 Table 2-3 Binary addition and subtraction table
44
Hexadecimal Addition C110 0 X19B9 16 Y+C7E6 16 X+YX+YE19F 16 44 C1100 X19119 Y+127146 X+YX+Y 1725F 1416+116+915 E19F
45
2.5 Representation of Negative Numbers
46
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 negative A 0 sign bit indicates a positive number A 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 –(2 n–1 –1) through +(2 n–1 –1) 47
48
Signed-Magnitude Representation Examples 01010101 2 = +85 10 11010101 2 = –85 10 01111111 2 = +127 10 11111111 2 = –127 10 00000000 2 = +0 10 10000000 2 = –0 10 48
49
Signed-Magnitude Representation Addition & Subtraction 49 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.
50
2.5.2 Complement Number Systems 2.5.3 Radix-Complement Representation 2.5.4 Two’s-Complement Representation 2.5.5 Diminished Radix-Complement Representation 2.5.6 One’s-Complement Representation 2.5.7 Excess Representations
51
Vocabulary Complement (n.) 補充、補充物、補數 Compliment (n.) (v.) 讚美 兩個字發音相同
52
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. 10110010Binary Number ↓↓↓↓↓↓↓↓ 010011011’s Complement
53
Figure 2–2 Example of inverters used to obtain the 1’s complement of a binary number. Thomas L. Floyd Digital Fundamentals, 9e Copyright ©2006 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved. 1’s Complement (cont’d)
54
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. 10110010Binary Number 010011011’s complement + 1Add 1 010011102’s complement
55
Figure 2–3 Example of obtaining the 2’s complement of a negative binary number. Thomas L. Floyd Digital Fundamentals, 9e Copyright ©2006 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved. 2’s Complement (cont’d)
56
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!
57
Signed-Magnitude Representation For example: (+25) 10 00011001 (-25) 10 10011001 00011001 ↑↑ Sign bitMagnitude bits
58
Signed-Magnitude Representation The Sign-Magnitude Representation has two binary numbers for (0) 10 (+0) 10 00000000 (-0) 10 10000000
59
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
60
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 61
62
Ranges of Different Number System Representations m -bit number Two’s Complement Representation –(2 m–1 ) to +(2 m–1 – 1) One’s Complement Representation –(2 m–1 – 1) to +(2 m–1 – 1) Signed-Magnitude Representation –(2 m–1 – 1) to +(2 m–1 – 1) Excess- 2 m–1 Representation –(2 m–1 ) to +(2 m–1 – 1) 62
63
Excess- 2 m-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- 2 m-1 representations, B equals 2 m-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 = 2 m-1 ). 63
64
2.6 Two’s-Complement Addition and Subtraction
65
2’s-Complement Addition and Subtraction 65 Figure 2-3 A modular counting representation of 4-bit two’s-complement numbers.
66
2’s-Complement Addition 66 +30011 ++4+0100 +70111 –2–2 1110 + –6–6 +1010 –8–8 11000 +60110 + –3–3 +1101 +310011 +40100 + –7–7 +1001 –3–3 1101
67
2’s-Complement Addition - Overflow 67 –3–3 1101 + –6–6 +1010 –9–9 10111 –8–8 1000 + –8–8 + – 16 10000 +50101 ++6+0110 +111011 +70111 ++7+0111 +141110 = +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’. 68 2’s-Complement Addition - Overflow
69
2’s-Complement Subtraction 69 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 70 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 ( C in ) of 1 instead of 0. See examples.
71
2’s-Complement Subtraction An Example 71 1 C in +40100 –+3–0011+1100 +11 0001
72
2’s-Complement Subtraction An Example 72 1 C in +30011 ––4–4–1100+0011 +7 0111
73
1 C in –51011 –4–0100+1011 –9–9 1 0111 2’s-Complement Subtraction - Overflow 73 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:
74
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.
75
Determine the decimal value of this signed binary number expressed in sign-magnitude representation: 10010101. EXAMPLE 2-15 ( Signed-Magnitude Rep. Decimal Value )
76
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 )
77
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 )
78
Numbers Are Added Two at a Time EXAMPLE 2-19 Add the signed numbers: 01000100, 00011011, 00001110, and 00010010.
79
Arithmetic Operations with Signed Numbers - Subtraction Related Problem for EXAMPLE 2-20: Subtract 01000111 from 01011000.
80
2.10 Binary Codes for Decimal 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 2 n valid code words. At least 4 bits are needed to represent the ten decimal digits. Binary-coded decimal (BCD) 81
82
Decimal Codes 82
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 83
84
Decimal BCD EXAMPLE 2-33 Convert each of the following decimal numbers to BCD: (a)35 (b)98 (c)170 (d)2469
85
BCD Decimal EXAMPLE 2-34 Convert each of the following BCD codes to decimal: (a)10000110 (b)001101010001 (c)1001010001110000
86
BCD Addition 4 個 bits 一組,每組超過 9 (1001 2 ) 要加 6 (0100 2 )
87
BCD Addition EXAMPLE 2-35 Add the following BCD numbers: (a)0011 + 0100 (b)00100011 + 00010101 (c)10000110 + 00010011 (d)010001010000 + 010000010111
88
BCD Addition EXAMPLE 2-36 Add the following BCD numbers: (a)1001 + 0100 (b)1001 + 1001 (c)00010110 + 00010101 (d)01100111 + 01010011
89
2.11 Gray Code
90
A Mechanical Encoding Disk Using a 3-bit Binary Code 90
91
A Mechanical Encoding Disk Using a 3-bit Gray Code 91
92
3-bit Binary Code & Gray Code 92
93
Constructing a Gray Code A 1-bit Gray code has two code words, 0 and 1. The first 2 n 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 2 n 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. 93
94
Constructing a Gray Code 1-bit Gray Code2-bit Gray Code3-bit Gray Code 0101 00 01 11 10 000 001 011 010 110 111 101 100 94
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.) 95
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. 96
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. 97
98
2.12 Character Codes
99
ASCII American Standard Code for Information Interchange Each character is represented by a 7-bit string 2 7 = 128 (total number of different characters) “Yeccch!” 1011001 1100101 1100011 1100011 1100011 1101000 0100001 99
100
ASCII 100
101
ASCII (cont’d) 101
102
2.13 Codes for Actions, Conditions, and States
103
States in a Traffic-Light Controller 103
104
Control Structure for a Digital System 104
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.