Digital Systems and Logic Design 1 General Overview This chapter is Number Systems, Operations, and Codes. Decimal numbers Binary numbers Decimal-to-binary numbers Binary arithmetic 1’s and 2’s Complements of Binary numbers Signed number Arithmetic operations with signed numbers Hexadecimal Numbers Octal Numbers Binary Coded Decimal (BCD) Digital Codes and Parity Digital System Application 2 Chapter Objectives Convert the number between decimal and binary. Apply arithmetic operation to binary numbers. Understand Binary Coded Decimal (BCD), Gray Code, ASCII 3 Decimal Numbers Decimal numbers has ten digits. (0, 1, 2, 3, …, 9) The decimal numbering system has a base of 10 with each position weighted by a factor of 10: 4 Binary Numbers Binary numbers has two digits, 1 and 0. The binary numbering system has a base of 2 with each position weight by a factor of 2. 5 Binary numbers: Example 10010 in binary is 1x24 + 0x23 + 0x22 + 1x21 + 0x20 = 18 Binary is the base 2 number system Most common in digital electronics Weight: Most Significant bit (MSB) Least Significant bit (LSB) 10010 MSB LSB 6 Integers and Fractional Parts Binary numbers can contain fractional parts as well as integer parts # quantization error. 7 Conversion: Decimal to Binary (Method 1) The decimal numbers is simply expressed as a sum of power of 2, and then 1s and 0s are written in the appropriate bit positions. 50 5010 = 32 + 18 = 32 + 16 + 2 = 1x25 + 1x24 + 1x21 = 1100102 322 = ? 8 Conversion: Decimal to Binary (Method 2) Repeated division 50/2 = 25/2 = 12/2 = 6/2 = 3/2 = 1/2 = Quotient 25 12 6 3 1 0 stop Remainder 0 LSB 1 0 0 1 1 MSB 50 = 110010 9 Conversion: Binary to Decimal The simplest way is to represent the binary number as anx2n + .. + a2x22 + a1x21 + a0x20 The conversion can be done by substituting the a’s with the given bits then multiplying and adding: 110010 = 1x25 + 1x24 + 1x21 = 50 10 Example Convert the binary whole number 100011012 to decimal 100011012 = 27 + 23 + 22 + 20 = 128 + 8 + 4 + 1 = 14110 11 Example Convert the fractional binary number 0.10112 to decimal 0.1011012 = 2-1 + 2-3 + 2-4 + 2-6 = 0.5 + 0.125 + 0.0625 = 0.687510 12 Example Convert the binary whole number 101.01012 to decimal 101.01012 = 22 + 20 + 2-2 + 2-4 = 4 + 1 +0.25 + 0.125 + 0.0625 = 5.437510 13 Conversion: Fractional to Binary ิ เป็ น เลขทศนิยมฐานสบ เลขฐานสอง 0.782 = 0.110012 ผลบวกของค่าน้ าหนัก การคูณซา้ ด ้วยสอง ผลบวกของค่านา้ หนัก : การคูณซา้ ด้วยสอง : 0.782 = 0.5 + 0.25 + 0.03125 = (0.78125) 0.782 x 2 = 1.564 = 2-1 + 2-2 + 2-5 0.128 x 2 = 0.256 = 0.110012 0.256 x 2 = 0.512 0.564 x 2 = 1.128 0.512 x 2 = 1.024 14 Binary Number Systems Unsigned Binary Code Signed Binary Codes 2’s Complement System BCD Code Excess Codes Floating-Point System 15 Unsigned Binary Code Given a number N in Unsigned Binary code, find the value of N in the decimal system Use series substitution method Given a number N in the decimal system, find the value of N in the Unsigned Binary Code. Use successive division method (for integer part) Use successive multiplication method (for fraction part) 16 Unsigned Binary Code Example 1: Represent (26)10 in Unsigned Binary Code 2610 = 11010 Example 2: Represent (26)10 in Unsigned Binary Code using 8 bits. 2610 = 00011010 Example 3: Represent (26)10 in Unsigned Binary Code using 4 bits. Can’t do. Not enough bits. 17 Unsigned Binary Code ( 4 bits) Unsigned Decimal 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 18 Unsigned Binary Code The Unsigned Binary Code is used to represent positive integer numbers. What is the range of values that can be represented with n bits in the Unsigned Binary Code? [0, 2n-1] How many bits are required to represent a given number N? number of bits = smallest integer greater than or equal to log(N) 19 Unsigned Binary Code: Arithmetic & Logic Operations Arithmetic Operations: Addition Subtraction Multiplication Division Logic Operations AND OR NOT XOR CONJUNCTION DISJUNCTION NEGATION EXCLUSIVE OR 20 Signed Binary Codes These are codes used to represent positive and negative numbers. Sign and Magnitude Code 1’s Complement Code 2’s Complement Code 21 Sign & Magnitude Code The leftmost bit is the sign bit 0 for positive numbers 1 for negative numbers The remaining bits represent the magnitude of the number Example: Sign & Mag. Code 01101 11101 00101 10101 Decimal +13 -13 +5 -5 22 Sign &Magnitude (4 bits) Unsigned Decimal 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 -0 -1 -2 -3 -4 -5 -6 -7 What is the range of values that can be represented in S&M code with n bits? 23 Sign&Magnitude Example 1: Represent (26)10 in Sign & Magnitude Code. 2610 = 011010 Example 2: Represent (26)10 in Sign & Magnitude Code using 8 bits 2610 = 0001 1010 Example 3: Represent (26)10 in Sign & Magnitude Code using 5 bits. Need at least 6 bits. 24 Sign&Magnitude Example 1: Represent (-26)10 in Sign & Magnitude Code. 26 = 11010 -2610 = 111010 Example 2: Represent (-26)10 in Sign & Magnitude Code using 8 bits 26 = 00011010 - 2610 = 10011010 Example 3: Represent (-26)10 in Sign & Magnitude Code using 5 bits. Need at least 6 bits. 25 Binary Arithmetic Addition Subtraction Multiplication Division 26 Binary Addition Equation A0 B0 0 COUT Recall decimal addition Binary addition 27 Example Add the following binary number: (a) 10010011 + 01001011 (b) 00011111 + 00000111 1 1 (a) (b) 10010011 +01001011 1 1 0 1 1 1 10 1 1 1 1 1 0001 1111 +00000111 0 0 1 0 0 1 10 147 + 75 = 222 31 +7 = 3810 28 Binary Subtraction 0-0=0 0-1=1 1-0=1 1-1=0 ผลลบ 0 ตัวยืม 0 ผลลบ 1 ตัวยืม 1 ผลลบ 1 ตัวยืม 0 ผลลบ 0 ตัวยืม 0 สมการทัว่ ไป : A0 B0 R0 BOUT R0 : ผลต่าง Bout : ตัวยืม ตารางความจริงการลบ A0 B0 0 0 0 1 1 0 1 1 R0 B0ut 0 0 1 1 1 0 0 0 A0 < B 0 ต้องการ ตัวยืม 29 30 Binary Multiplication A Basic Roles for Multiplication 31 32 Binary Division 33 1’s Complement Code Positive numbers: same as in unsigned binary code pad a 0 at the leftmost bit position Negative numbers: 1. Represent the magnitude of the number in unsigned binary system 2. pad a 0 at the leftmost bit position 3. complement every bit 34 1’s Complement Code Example: represent 2610 in 1’s complement code 2610 = 11010 = 011010 Pad a 0: Example: Represent (-26)10 in 1’s complement code. 1. 2. Pad a 0: 3. Complement: 26 = 11010 011010 100101 35 1’s Complement Code Example: Represent (-26)10 in 1’s comp. code using 8 bits 1. Represent magnitude in unsigned binary using 8 bits 26 = 0001 1010 2. Complement every bit 11100101 -2610 = (1110 0101) 1’s comp 36 1’s Complement Code (4 bits) Unsigned Decimal 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 -7 -6 -5 -4 -3 -2 -1 -0 What is the range of values that can be represented in S&M code with n bits? [ -(2 (n-1) -1) , 2 (n-1) -1] 37 2’s Complement Code This is the code commonly used to represent integer numbers. Positive Numbers: same as in unsigned binary code pad with a 0 leftmost bit position Negative Numbers 1. represent magnitude in unsigned binary code 2. pad leftmost positions with 0s 3. complement every bit 4. add 1 38 2’s Complement Code Example 1: Represent 26 in 2’s complement code. 26 = 011010 Example 2: Represent 26 in 2’s complement code using 8 bits 26 = 00011010 Example 3: Represent 26 in 2’s complement code using 5 bits Need at least 6 bits. 39 2’s Complement Code Example 3: Represent - 26 in 2’s comp. Code 1. 2. Pad with a 0: 3. Complement: 4. Add 1: +26 = 11010 011010 100101 + 1 --------------100110 40 2’s Complement Code Example 4: Represent - 26 in 2’s comp. Code using 8 bits 1. 2. Pad 0s: 3. Complement: 4. Add 1: +26 = 11010 00011010 11100101 + 1 --------------11100110 41 2’s Complement Code More example: represent 65 in 2’s comp. Code. 65 = (0100 0001)2’s comp Represent - 65 in 2’s comp 65 = 0100 0001 -65 = 1011 1111 42 Conversion from 2’s comp code to decimal code How to convert a number in 2’s Comp. Code into the decimal code. There 2 cases: Case 1: If leftmost bit of the number is 0 => number is positive => conversion is the same as in unsigned binary code 43 Conversion from 2’s comp code to decimal code Case 2: If leftmost bit is 1 => the number is negative step1: complement every bit step2: add 1 step3: convert result to decimal code using same method as in unsigned binary code. Answer = the negative of the result of step 3. 44 2’s Complement Code (4 bits) Unsigned Decimal 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 0 1 2 3 4 5 6 7 -8 -7 -6 -5 -4 -3 -2 -1 Range of values with n bits: [ -2 (n-1) , 2 (n-1) -1] 45 1’s and 2’s Complement of Binary Numbers 46 47 Alternative Method to find 2’s Complement 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 Hexadecimal Notation Hexadecimal system: base 16 There are 16 digits: 0 123456789ABCDEF Each Hex digit represents a group of 4 bits (i.e. half of a byte) 0000 thru 1111 Generally used as shorthand notation for binary numbers => easier to read Binary: Decimal: Hex: 0101 1010 5 10 5 A 1001 9 9 1110 14 E 65 Hexadecimal number 66 Hexadecimal number conversions 67 68 69 70 71 Addition in Hex. 72 Subtration in Hex. 73 74 Octal number 75 76 77 78 79 Binary Coded Decimal (BCD) Decimal numbers are more natural to humans. Binary numbers are natural to computers. Quite expensive to convert between the two. If little calculation is involved, we can use some coding schemes for decimal numbers. One such scheme is BCD, also known as the 8421 code. Represent each decimal digit as a 4-bit binary code. 80 Binary Coded Decimal (BCD) Decimal digit BCD Decimal digit BCD 0 0000 5 0101 1 0001 6 0110 2 0010 7 0111 3 0011 8 1000 4 0100 9 1001 Some codes are unused, eg: (1010)BCD, (1011) BCD, …, (1111) BCD. These codes are considered as errors. Easy to convert, but arithmetic operations are more complicated. Suitable for interfaces such as keypad inputs and digital readouts. CS1104-2 Binary Coded Decimal (BCD) 81 Binary Coded Decimal (BCD) Decimal digit BCD Decimal digit BCD 0 0000 5 0101 1 0001 6 0110 2 0010 7 0111 3 0011 8 1000 4 0100 9 1001 Examples: (234)10 = (0010 0011 0100)BCD (7093)10 = (0111 0000 1001 0011)BCD (1000 0110)BCD = (86)10 (1001 0100 0111 0010)BCD = (9472)10 Notes: BCD is not equivalent to binary. Example: (234)10 = (11101010)2 CS1104-2 Binary Coded Decimal (BCD) 82 BCD : Binary Coded Decimal 83 84 85 86 The Gray Code Unweighted (not an arithmetic code). Only a single bit change from one code number to the next. Good for error detection. Decimal 0 1 2 3 4 5 6 7 Binary 0000 0001 0010 0011 0100 0101 0110 0111 Gray Code 0000 0001 0011 0010 0110 0111 0101 0100 Decimal 8 9 10 11 12 13 14 15 Binary 1000 1001 1010 1011 1100 1101 1110 1111 Gray code 1100 1101 1111 1110 1010 1011 1001 1000 Q. How to generate 5-bit standard Gray code? Q. How to generate n-bit standard Gray code? CS1104-2 The Gray Code 87 The Gray Code 0000 0001 0011 0001 0010 0000 0010 0110 0111 0011 0101 0001 0100 0000 1100 0100 0101 1101 0111 1111 0110 1110 0010 1010 0011 1011 0001 1001 0000 1000 Generating 4-bit standard Gray code. CS1104-2 The Gray Code 88 The Gray Code sensors mis-aligned sensors Binary coded: 111 110 000 CS1104-2 The Gray Code mis-aligned sensors Gray coded: 111 101 89 Binary-to-Gray Code Conversion Retain most significant bit. From left to right, add each adjacent pair of binary 1 1 code bits to get the next Gray code bit, discarding carries. Example: Convert binary number 10110 to Gray 1 0 + 1 1 0 Binary 1 + 0 1 1 0 Binary 0 1 1 0 Binary code. 1 Gray 1 1 0 1 1 1 + 1 0 0 Binary Gray 1 1 Gray 1 1 0 1 1 1 1 0 + 1 1 0 Binary 1 Gray Gray (10110)2 = (11101)Gray CS1104-2 Binary-to-Gray Code Conversion 90 Gray-to-Binary Conversion Retain most significant bit. From left to right, add each binary code bit generated to the Gray code bit in the next position, discarding carries. Example: Convert Gray code 11011 to binary. 1 1 0 1 1 1 Gray 1 + 1 1 Binary 1 1 0 1 + 1 0 0 1 1 0 1 1 1 Binary 1 1 0 1 + Binary 1 0 + 0 Gray 1 Gray 1 0 0 1 0 1 Gray 0 Binary 0 1 1 Gray Binary (11011)Gray = (10010)2 CS1104-2 Gray-to-Binary Conversion 91 Error Detection Codes Sometimes, it is not enough to do error detection. We may want to do error correction. Error correction is expensive. In practice, we may use only single-bit error correction. Popular technique: Hamming Code (not covered). CS1104-2 Error Detection Codes 92 Gray code 93 Binary to Gray Conversion 94 95 Gray to Binary 96 97 Other Decimal Codes Decimal Digit 0 1 2 3 4 5 6 7 8 9 BCD 8421 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 Excess-3 84-2-1 2*421 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 0000 0111 0110 0101 0100 1011 1010 1001 1000 1111 0000 0001 0010 0011 0100 1011 1100 1101 1110 1111 Biquinary 5043210 0100001 0100010 0100100 0101000 0110000 1000001 1000010 1000100 1001000 1010000 Self-complementing codes: excess-3, 84-2-1, 2*421 codes. Error-detecting code: biquinary code (bi=two, quinary=five). CS1104-2 Other Decimal Codes 98 Self-Complementing Codes Examples: excess-3, 84-2-1, 2*421 codes. The codes that represent the pair of complementary digits are complementary of each other. Excess-3 code 0: 0011 1: 0100 2: 0101 3: 0110 4: 0111 5: 1000 6: 1001 7: 1010 8: 1011 9: 1100 CS1104-2 Self-Complementing Codes 241: 0101 0111 0100 758: 1010 1000 1011 99 Alphanumeric Codes Apart from numbers, computers also handle textual data. Character set frequently used includes: alphabets: digits: special symbols: non-printable: ‘A’ .. ‘Z’, and ‘a’ .. ‘z’ ‘0’ .. ‘9’ ‘$’, ‘.’, ‘,’, ‘@’, ‘*’, … SOH, NULL, BELL, … Usually, these characters can be represented using 7 or 8 bits. CS1104-2 Alphanumeric Codes 100 Alphanumeric Codes Two widely used standards: ASCII (American Standard Code for Information Interchange) EBCDIC (Extended BCD Interchange Code) ASCII: 7-bit, plus a parity bit for error detection (odd/even parity). EBCDIC: 8-bit code. CS1104-2 Alphanumeric Codes Character 0 1 ... 9 : A B ... Z [ \ ASCII Code 0110000 0110001 ... 0111001 0111010 1000001 1000010 ... 1011010 1011011 1011100 101 Alphanumeric Codes ASCII table: LSBs 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 CS1104-2 000 NUL SOH STX ETX EOT ENQ ACK BEL BS HT LF VT FF CR O SI 001 DLE DC1 DC2 DC3 DC4 NAK SYN ETB CAN EM SUB ESC FS GS RS US 010 SP ! “ # $ % & ‘ ( ) * + , . / MSBs 011 100 0 @ 1 A 2 B 3 C 4 D 5 E 6 F 7 G 8 H 9 I : J ; K < L = M > N ? O Alphanumeric Codes 101 P Q R S T U V W X Y Z [ \ ] ^ _ 110 ` a b c d e f g h i j k l m n o 111 p q r s t u v w x y z { | } ~ DEL 102 Error Detection Codes Errors can occur data transmission. They should be detected, so that re-transmission can be requested. With binary numbers, usually single-bit errors occur. Example: 0010 erroneously transmitted as 0011, or 0000, or 0110, or 1010. Biquinary code uses 3 additional bits for error-detection. For single-error detection, one additional bit is needed. CS1104-2 Error Detection Codes 103 Error Detection Codes Parity bit. Even parity: additional bit supplied to make total number of ‘1’s even. Odd parity: additional bit supplied to make total number of ‘1’s odd. Character ASCII Code Example: Odd parity. CS1104-2 0 1 ... 9 : A B ... Z [ \ Error Detection Codes 0110000 1 0110001 0 ... 0111001 1 0111010 1 1000001 1 1000010 1 ... 1011010 1 1011011 0 1011100 1 Parity bits 104 Error Detection Codes Parity bit can detect odd number of errors but not even number of errors. Example: For odd parity numbers, 10011 10001 (detected) 10011 10101 (non detected) Parity bits can also be applied to a block of data: 0110 1 0001 0 1011 0 1111 1 1001 1 0101 0 Column-wise parity Row-wise parity CS1104-2 Error Detection Codes 105 Error Detection Codes Sometimes, it is not enough to do error detection. We may want to do error correction. Error correction is expensive. In practice, we may use only single-bit error correction. Popular technique: Hamming Code (not covered). CS1104-2 Error Detection Codes 106 Alphanumeric Codes 107 ASCII 108 109 Parity Method for error checking 110 111 Put it together 112