Digital Design Principles and Practices Chapter 2 Number
Digital Design: Principles and Practices Chapter 2 Number Systems and Codes
2. 1 Positional Number Systems
Counting in Binary l With n bits, you can count from 0 to 2 n – 1 l For example, with four bits (n=4), you can count from 0 to 15: 24 – 1 = 16 – 1 = 15 3
Counting in Binary – An Application Figure 2– 1 Illustration of a simple binary counting application. 4
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
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*101 + d 0*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 § 二進位的小數點 6
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
Positional Number System - Binary • Radix = r = 2 • General form: • The value: 8
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
Binary to Decimal Conversion • 100112 = 1910 Ø 100112 = 1*16 + 0*8 + 0*4 + 1*2 + 1*1 = 1910 • 1000102 = 3410 • 101. 0012 = 5. 12510 10
2. 2 Octal and Hexadecimal Numbers
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
Binary, Decimal, Octal, & Hexadecimal Table 2 -1 13
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: Ø 100011102 = 100 011 001 1102 = 43168 • Example #2: Ø 11101101010012 = 011 101 110 101 0012 = 3556518 14
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: Ø 100011102 = 1000 11102 = 8 CE 8 • Example #2: Ø 11101101010012 = 0001 1101 1010 10012 = 1 DBA 98 15
Binary-to-Octal Conversion & Binary-to-Hexadecimal Conversion • Example: Ø 10. 10110010112 = 010. 101 1002 = 2. 54548 = 0010. 1011 0010 11002 = 2. B 2 C 16 16
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 110. 001 1112 BEAD 16 = 1011 1110 1010 11012 9 F. 46 C 16 = 1001 1111. 0100 0110 11002 17
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 “ 0 x” to denote a hexadecimal number, for example, 0 x. BFC 0000. 18
2. 3 General Positional-Number. System Conversions
Number System Conversions • In general, conversion between two radices cannot be done by simple substitutions; arithmetic operations are required. • In this section, we show 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
Radix r to Radix 10 Conversion Examples • 1 CE 816 = 1*163 + 12*162 + 14*161 + 8*160 = 740010 • F 1 A 316 = 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: Ø F 1 A 316 = ( ( (15)*16 + 10 )*16 + 3 = 6185910 21
Binary, Decimal, Octal, & Hexadecimal Table 2 -1 22
Radix r to Radix 10 Conversion - An Alternative Method • General Form: • Example: Ø F 1 A 316 = ( ( (15)*16 + 10 )*16 + 3 = 6185910 • The method can be used to convert a decimal number D to a radix r. 23
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
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) 6185910 = F 1 A 316 25
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) 17910 = 101100112 26
Conversion Methods for Common Radices (I) Table 2 -1 27
Conversion Methods for Common Radices (II) Table 2 -1 (cont’d) 28
Hexadecimal-to-Decimal Conversion EXAMPLE 2 -26 Convert the following hexadecimal numbers to decimal. (a) 1 C 16 (b) A 8516 Ans: (a) 2810 (b) 269310
Decimal-to-Hexadecimal Conversion EXAMPLE 2 -28 Convert the decimal number 650 to hexadecimal. Ans: 28 A 16
Octal-to-Decimal Conversion Example: (2374)8 = ( Ans: 127610 )10
Decimal-to-Octal Conversion Example: (359)10 = ( Ans: 5478 )8
Octal-to-Binary Conversion EXAMPLE 2 -31 Convert 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 -32 Convert each of the following binary numbers to octal: (a) 110101 (b) 101111001 (c) 10011010 (d) 11010000100 Ans: (a) 658 (b) 5718 (c) 46328 (d) 32048
2. 4 Addition and Subtraction of Non-decimal Numbers
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 36
Binary Addition & Subtraction Table 2 -3 Binary addition and subtraction table 37
Binary Addition 38
Binary Addition C Ø s: sum Ø cin: input carry Ø cout: output carry 1 0 1 1 0 0 0 X 190 Y + 141 X+Y • Binary Addition 331 1 0 1 1 1 0 + 1 0 0 0 1 1 0 1 0 1 1 39
Binary Subtraction 40
Binary Subtraction (cont’d) • Binary Subtraction (minuend minus subtrahend yields difference) Ø d: difference Ø bin: input borrow Ø bout: output borrow 41
Binary Subtraction B • Binary Subtraction Ø d: difference Ø bin: input borrow Ø bout: output borrow 0 1 1 0 1 0 X 210 Y - 109 X-Y 101 1 1 0 0 1 0 - 0 1 1 0 0 1 42
Binary Addition & Subtraction Table 2 -3 Binary addition and subtraction table 43
Hexadecimal Addition C X Y X+Y + + 1 1 C E 1 9 7 1 0 B E 9 0 916 616 F 16 1 1 12 14 1 9 7 17 0 11 14 25 0 9 6 F 14 E 16+1 1 16+9 9 15 F 44
2. 5 Representation of Negative Numbers
2. 5. 1 Signed-Magnitude Representation
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
Signed-Magnitude Representation Examples • • • 01012 110101012 011111112 00002 100000002 = = = +8510 – 8510 +12710 – 12710 +010 – 010 48
Signed-Magnitude Representation Addition & 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. 49
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
1’s Complement • How do you calculate the 1’s complement of a binary number? Ans: Change all 1 s to 0 s and all 0 s to 1 s • For example: The 1’s complement of the binary number 10110010 is 01001101. 1 0 1 1 0 0 1 0 ↓ ↓ ↓ ↓ 0 1 0 0 1 1 0 1 Binary Number 1’s Complement
1’s Complement (cont’d) Figure 2– 2 Thomas L. Floyd Digital Fundamentals, 9 e Example of inverters used to obtain the 1’s complement of a binary number. Copyright © 2006 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.
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 01001101 + 1 01001110 Binary Number 1’s complement Add 1 2’s complement
2’s Complement (cont’d) Figure 2– 3 Thomas L. Floyd Digital Fundamentals, 9 e Example of obtaining the 2’s complement of a negative binary number. Copyright © 2006 by Pearson Education, Inc. Upper Saddle River, New Jersey 07458 All rights reserved.
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 1001 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 0000 (-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 0000 (-0)10 1111
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 0000 (-1)10 1111 (-128)10 10000000
Decimal and 4 -bit Numbers 61
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
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
2. 6 Two’s-Complement Addition and Subtraction
2’s-Complement Addition and Subtraction Figure 2 -3 A modular counting representation of 4 -bit two’s-complement numbers. 65
2’s-Complement Addition +3 + +4 +7 0011 + 0100 0111 – 2 + – 6 – 8 1110 + 1010 11000 +6 + – 3 +3 0110 + 1101 10011 +4 + – 7 – 3 0100 + 1001 1101 66
2’s-Complement Addition - Overflow – 3 – 6 – 9 1101 + 1010 10111 = +7 +5 + +6 +11 0101 + 0110 1011 = -5 – 8 + – 8 – 16 1000 + 10000 = +0 +7 +14 0111 + 0111 1110 = -2 + 67
2’s-Complement Addition - Overflow • 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 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 69
2’s-Complement Subtraction • Negating the subtrahend adding the minuend can be accomplished with only one addition operation as follows: Ø Perform a bit-by-bit complement of the subtrahend add the complemented subtrahend to the minuend with an initial carry (Cin) of 1 instead of 0. Ø See examples. 70
2’s-Complement Subtraction An Example +4 – +3 +1 0100 – 0011 1 Cin 0100 + 1100 1 0001 71
2’s-Complement Subtraction An Example +3 – – 4 +7 0011 – 1100 + 1 Cin 0011 0111 72
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: – – 5 4 – 9 1011 – 0100 1 Cin 1011 + 1011 1 0111 73
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.
EXAMPLE 2 -15 ( Signed-Magnitude Rep. Decimal Value ) Determine the decimal value of this signed binary number expressed in sign-magnitude representation: 10010101.
EXAMPLE 2 -16 ( 1’s Complement Rep. Decimal Value ) Determine the decimal values of the signed binary numbers expressed in 1’s complement representation: (a) 00010111 (b) 11101000
EXAMPLE 2 -17 ( 2’s Complement Rep. Decimal Value ) Determine the decimal values of the signed binary numbers expressed in 2’s complement representation: (a) 01010110 (b) 1010
Numbers Are Added Two at a Time EXAMPLE 2 -19 Add the signed numbers: 0100, 00011011, 00001110, and 00010010.
Arithmetic Operations with Signed Numbers - Subtraction Related Problem for EXAMPLE 2 -20: Subtract 01000111 from 01011000.
2. 10 Binary Codes for Decimal Numbers
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
Decimal Codes 82
Decimal Codes • BCD = 8421 code • 2421 code Ø Self complementing: The code word for the 9 s’ 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
Decimal BCD EXAMPLE 2 -33 Convert each of the following decimal numbers to BCD: (a) 35 (b) 98 (c) 170 (d) 2469
BCD Decimal EXAMPLE 2 -34 Convert each of the following BCD codes to decimal: (a) 10000110 (b) 001101010001 (c) 1001010001110000
BCD Addition l 4 個 bits 一組,每組超過 9 (10012) 要加 6 (01002)
BCD Addition EXAMPLE 2 -35 Add the following BCD numbers: (a) 0011 + 0100 (b) 00100011 + 00010101 (c) 10000110 + 00010011 (d) 010001010000 + 010000010111
BCD Addition EXAMPLE 2 -36 Add the following BCD numbers: (a) 1001 + 0100 (b) 1001 + 1001 (c) 00010110 + 00010101 (d) 01100111 + 01010011
2. 11 Gray Code
A Mechanical Encoding Disk Using a 3 -bit Binary Code 90
A Mechanical Encoding Disk Using a 3 -bit Gray Code 91
3 -bit Binary Code & Gray Code 92
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
Constructing a Gray Code 1 -bit Gray Code 0 1 2 -bit Gray Code 00 01 11 10 3 -bit Gray Code 000 001 010 111 100 94
Binary code word Gray-code word • The bits of an n-bit binary or Gray-code word are numbered from right to left, from 0 to n – 1. • Bit i of a Gray-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
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
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
2. 12 Character Codes
ASCII • American Standard Code for Information Interchange • Each character is represented by a 7 -bit string • 27 = 128 (total number of different characters) • “Yeccch!” Ø 101100101 1100011 110100001 99
ASCII 100
ASCII (cont’d) 101
2. 13 Codes for Actions, Conditions, and States
States in a Traffic-Light Controller 103
Control Structure for a Digital System 104
- Slides: 104