Digital Design: Principles and Practices Chapter 2 Number Systems and Codes
2.1 Positional Number Systems
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
Figure 2–1 Illustration of a simple binary counting application. Counting in Binary – An 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* * *10 + 4*1 Each weight is a power of 10 corresponding to the digit’s position. Example #2: = 5* * *10 + 5*1 + 6* *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 * d 0 * d -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 = = 1*16 + 0*8 + 0*4 + 1*2 + 1*1 = = =
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
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: = = Example #2: = =
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: = = 8CE 8 Example #2: = = 1DBA9 8 15
Binary-to-Octal Conversion & Binary-to-Hexadecimal Conversion Example: = = = = 2.B2C 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: = = BEAD 16 = 9F.46C 16 =
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, 0xBFC
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 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
Radix r to Radix 10 Conversion Examples 1CE8 16 = 1* * * *16 0 = F1A3 16 = 15* * * *16 0 = = 4* * * *8 -1 = = 1* * * *4 -1 = An alternative method: F1A3 16 = ( ( (15)*16 + 1)* )* =
Binary, Decimal, Octal, & Hexadecimal Table
Radix r to Radix 10 Conversion - An Alternative Method General Form: Example: F1A3 16 = ( ( (15)*16 + 1)* )* = 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) ÷ 16 = 3866 …… 3 (LSB) ÷ 16 = 241 …… 10 ÷ 16 = 15 …… 1 ÷ 16 = 0 …… 15 (MSB) = F1A
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) =
Conversion Methods for Common Radices (I) 27 Table 2-1
Conversion Methods for Common Radices (II) 28 Table 2-1 (cont’d)
EXAMPLE 2-26 Convert the following hexadecimal numbers to decimal. (a)1C 16 (b)A85 16 Hexadecimal-to-Decimal Conversion Ans: (a) (b)
Decimal-to-Hexadecimal Conversion EXAMPLE 2-28 Convert the decimal number 650 to hexadecimal. Ans: 28A 16
Octal-to-Decimal Conversion Example: (2374) 8 = ( ) 10 Ans:
Decimal-to-Octal Conversion Example: (359) 10 = ( ) 8 Ans: 547 8
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) Ans: (a) (b) (c) (d)
Binary-to-Octal Conversion EXAMPLE 2-32 Convert each of the following binary numbers to octal: (a) (b) (c) (d) Ans: (a) 65 8 (b) (c) (d)
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 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
Binary Addition & Subtraction Table 37 Table 2-3 Binary addition and subtraction table
Binary Addition 38
Binary Addition 39 Binary Addition s : sum c in : input carry c out : output carry C X Y X + Y
Binary Subtraction 40
Binary Subtraction (cont’d) 41 Binary Subtraction (minuend minus subtrahend yields difference) d : difference b in : input borrow b out : output borrow
Binary Subtraction 42 Binary Subtraction d : difference b in : input borrow b out : output borrow B X Y X - Y
Binary Addition & Subtraction Table 43 Table 2-3 Binary addition and subtraction table
Hexadecimal Addition C110 0 X19B9 16 Y+C7E6 16 X+YX+YE19F C1100 X19119 Y X+YX+Y 1725F E19F
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 = = – = = – = = –
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.
2.5.2 Complement Number Systems Radix-Complement Representation Two’s-Complement Representation Diminished Radix-Complement Representation One’s-Complement Representation 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 is Binary Number ↓↓↓↓↓↓↓↓ ’s Complement
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 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 is Binary Number ’s complement + 1Add ’s complement
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 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 (-25) 10 ↑↑ Sign bitMagnitude bits
Signed-Magnitude Representation The Sign-Magnitude Representation has two binary numbers for (0) 10 (+0) 10 (-0) 10
1’s Complement Representation For example: (+25) 10 (the same as the Signed-Magnitude Rep.) (-25) 10 (1’s complement of (+25) 10 ) The 1’s Complement Rep. has two binary numbers for (0) 10 (+0) 10 (-0) 10
2’s Complement Representation For example: (+25) 10 (the same as the Signed-Magnitude Rep.) (-25) 10 (2’s complement of (+25) 10 ) The 2’s Complement Rep. has only one binary numbers for (0) 10 (+0) 10 (-1) 10 (-128) 10
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 65 Figure 2-3 A modular counting representation of 4-bit two’s-complement numbers.
2’s-Complement Addition –2– –6– –8– –3– –7– –3–3 1101
2’s-Complement Addition - Overflow 67 –3– –6– –9– –8– –8–8 + – = +7 = +0 = -5 = -2
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
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
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.
2’s-Complement Subtraction An Example 71 1 C in –+3–
2’s-Complement Subtraction An Example 72 1 C in ––4–4–
1 C in –51011 –4– –9– ’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:
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: 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) (b) ( 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) (b) ( 2’s Complement Rep. Decimal Value )
Numbers Are Added Two at a Time EXAMPLE 2-19 Add the signed numbers: , , , and
Arithmetic Operations with Signed Numbers - Subtraction Related Problem for EXAMPLE 2-20: Subtract from
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 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
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) (b) (c)
BCD Addition 4 個 bits 一組,每組超過 9 ( ) 要加 6 ( )
BCD Addition EXAMPLE 2-35 Add the following BCD numbers: (a) (b) (c) (d)
BCD Addition EXAMPLE 2-36 Add the following BCD numbers: (a) (b) (c) (d)
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 Code2-bit Gray Code3-bit Gray Code
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
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 2 7 = 128 (total number of different characters) “Yeccch!”
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