EE2174: Digital Logic and Lab Professor Shiyan Hu Department of Electrical and Computer Engineering Michigan Technological University CHAPTER 2 Number System Overview Digital Computers Number Systems Representations Conversions Arithmetic Operations Decimal Codes Alphanumeric Codes 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 2 Digital Systems Major characteristic manipulation of discrete elements of information (any set restricted to a finite # of elements) e.g. 10 decimal digits, 26 letters Discrete elements (in digital systems) can be represented by signals (physical quantities). 9-Apr-15 Most common signals: electrical (voltage, current) Chapter 1: Digital Computers and Information PJF - 3 Voltage Ranges The 2 binary values (HIGH, LOW) of a digital signal are represented by ranges of voltage values 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 4 Information Representation Binary signals (2 discrete values) 0 and 1 (LOW and HIGH, FALSE and TRUE) Binary quantity: binary digit/bit Information: group of bits/words (size: 8, 16, 32, 64, …) Digital hardware computes binary functions of binary numbers: Combinational (memoryless) Sequential (using memory) 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 5 Number Systems Representation of numbers Radix: “base”, the primitive unit for group of numbers, e.g. for decimal arithmetic radix=10 (“base” 10) For every system, we need arithmetic operations (addition, subtraction, multiplication) Also, conversion from one base to the other 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 6 Number Systems - Decimal “base” 10 (radix is 10) 10 digits: 0..9 (251.3)10 = 2102 + 5101 + 1100 + 310-1 Note: ‘.’ is called the radix point (decimal point for base 10) 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 7 Number Systems – Decimal (cont.) In general, a decimal number with n digits to the left of the decimal point, and m digits to the right of the decimal point is written as: An-1 An-2 … A1 A0 . A-1 A-2 … A-m+1 A-m where Ai is a coefficient between 0..9, and i denotes the weight (=10i) of Ai. 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 8 Number Systems – Decimal (cont.) The value of An-1 An-2 … A1 A0 . A-1 A-2 … A-m+1 A-m is calculated by i=n-1..0 (Ai 10i ) + i=-m..-1 (Ai 10i ) 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 9 Number Systems – General “base” r (radix r) r digits N = An-1 r n-1 + An-2r n-2 +… + A1r + A0 + A-1 r -1 + A-2r -2 +… + A-m r -m Most Significant Bit (MSB) 9-Apr-15 Least Significant Bit (LSB) Chapter 1: Digital Computers and Information PJF - 10 Number Systems – General (cont.) e.g. let r = 8 (312.5)8 = 382 + 181 + 280 + 58-1 = (202.625)10 Conversion from n-ary (any system with radix n) to decimal follows similar process as above 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 11 Number Systems (cont.) Most common number systems for computers: Binary (r = 2) Octal (r = 8) Hexadecimal (r = 16) 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 12 Binary Numbers-Base 2 Computers represent all data as “strings of bits”, each bit being either 0 or 1 “base” 2, with 2 digits: 0 and 1 e.g. (101101.10)2 = 125 + 024 + 123 + 122 + 021 + 120 + 12-1 + 02-2 (in decimal) = 32 + 0 + 8 + 4 + 0 + 1 + ½ + 0 = (45.5)10 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 13 Binary Numbers-Base 2 (cont.) e.g. (1001.011)2 = 123 + 022 + 021 + 120 + 02-1 + 12-2 + 12-3 (in decimal) = 8 + 1 + 0.25 + 0.125 = (9.375)10 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 14 Powers of two Memorize at least through 216 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 15 Octal Numbers-Base 8 “base” 8, with 8 digits: 0..7 e.g. (762)8 = 782 + 681 + 280 (in decimal) = 448 + 48 + 2 = (498)10 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 16 Hexadecimal Numbers-Base 16 r = 16 Digits (convention): 0..9, A, B, C, D, E, F A=10, B=11, … , F = 15 e.g. (3FB)16 = 3162 + 15161 + 11160 (in decimal) 9-Apr-15 = 768 + 240 + 11 = (1019)10 Chapter 1: Digital Computers and Information PJF - 17 Base Conversions Any base r decimal – Easy! (already covered, see slides 13-14, 16-17, 19-20) Decimal Binary Octal Binary Hex Binary Decimal Any base r 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 18 Decimal to Binary Let N be a decimal number. 1. 2. 3. Find the greatest number that is a power of 2 and when subtracted from N it produces a positive difference N1 Put a 1 in the MSB Repeat Step 1, starting from N1 and finding difference N2. Put a 1 in the corresponding bit. Stop when the difference is zero. 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 19 Decimal to Binary (cont.) e.g. N = (717)10 717 – 512 = 205 = N1 512 = 29 205 –128 = 77 = N2 128 = 27 77 – 64 = 13 = N3 64 = 26 13 – 8 = 5 = N4 8 = 23 5 – 4 = 1 = N5 4 = 22 1 – 1 = 0 = N6 1 = 20 (717)10 = 29 + 27 + 26 + 23 + 22 + 20 = ( 1 0 1 1 0 0 1 1 0 1)2 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 20 Binary to Octal and Hex Octal: 8 = 23 every 3 binary bits convert 1 octal Hex: 16 = 24 every 4 binary bits convert 1 hex 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 21 Binary Octal (011 010 101 000 . 111 101 011 100)2 (3 2 9-Apr-15 5 0 . 7 5 Chapter 1: Digital Computers and Information 3 4 )8 PJF - 22 Binary Hex ( 0110 1010 1000 . 1111 0101 1100 )2 ( 6 9-Apr-15 A 8 . F 5 Chapter 1: Digital Computers and Information C )16 PJF - 23 Octal Hex Go through Binary! Hex Binary Octal Octal Binary Hex 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 24 Convert Decimal to any base r Integer part: Divide by the base, keep track of remainder, and read-up e.g. (153)10 = ( ? )8 , r = 8 153 / 8 = 19 + 1/8 rem = 1 LSB 19 / 8 = 2 + 3/8 rem = 3 2 / 8 = 0 + 2/8 rem = 2 MSB stop 9-Apr-15 (153)10 = ( 231)8 Chapter 1: Digital Computers and Information PJF - 25 Convert Decimal to any base r Fractional part: Multiply by the base, keep track of integer part, and read-down e.g. (0.78125)10 = ( ? )16 , r = 16 0.7812516 = 12.5 integer = 12 = C MSB 0.5 16 = 8.0 integer = 8 = 8 LSB stop 9-Apr-15 (0.78125)10 = (0.C8)16 Chapter 1: Digital Computers and Information PJF - 26 Binary Arithmetic Operations: Addition Follow same rules as in decimal addition, with the difference that when sum is 2 indicates a carry (not a 10) Learn new carry rules 0+0 = 0c0 (sum 0 with carry 0) 0+1 = 1+0 = 1c0 Carry 1 1 1 1 1 0 1+1 = 0c1 Augend 0 0 1 0 0 1 1+1+1 = 1c1 Addend 0 1 1 1 1 1 Result 9-Apr-15 1 0 1 0 0 0 Chapter 1: Digital Computers and Information PJF - 27 Binary Arithmetic Operations: Addition (cont.) “Half addition” (rightmost bit position, aka LSB): only 2 bits are added, yielding a sum and a carry “Full addition” (remaining positions): three bits are added, yielding a sum and a carry In Chapter 3, we’ll see many different hardware implementations of half-adders and full-adders 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 28 Binary Arithmetic Operations: Subtraction Learn new borrow rules 0-0 = 1-1 = 0b0 (result 0 with borrow 0) 1-0 = 1b0 0-1 = 1b1 Borrow 1 1 0 0 … Minuend 1 1 0 1 1 Subtrahend 0 1 1 0 1 Result 9-Apr-15 0 1 1 1 0 Chapter 1: Digital Computers and Information PJF - 29 Keys to Success Recall and use the “algorithms” you use to perform base-10 arithmetic. Generalize them to the base in use (carry, borrow rules change) Preserve the base! In binary, 1+1=10 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 30 Two’s complement Two’s complement of a binary number is computed as complementing each bit and add 1 It is used for subtrahend in subtraction For example, (3)10=(011)2, so (-3)10=(100+1)2 (111)-(011)=(111)+(101)=(1100) removing the leading carry = (100) Why? 9-Apr-15 x-(011)=x-[111-100]=x-[1000-1-100]=x(100+1)-1000 Arithmetic PJF - 31 Binary Arithmetic Operations: Multiplication Shift-and-add algorithm, as in base 10 M’cand M’plier 0 0 0 0 0 0 1 0 1 1 0 1 1 0 (1) (2) (3) Sum 0 0 0 1 0 0 1 0 0 1 1 0 0 1 0 1 0 0 1 1 0 1 0 1 0 0 0 0 Check: 13 * 6 = 78 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 32 CODES Representations of info (set) obtained by associating one or more codewords (a binary pattern/string) with each element in the set n-bit binary code: a group of n bits that can n encode up to 2 distinct elements e.g. A set of 4 distinct numbers can be represented by 2-bit codes s.t. each number in the set is assigned exactly one of the combinations/codes in {00,01,10,11} 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 33 CODES (cont.) To encode m distinct elements with an n n-bit code: 2 >= m Note: The codeword associated with each number is obtained by coding the number, not converting the number to binary. We will see: BCD, ASCII, Unicode 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 34 Binary-Coded Decimal (BCD) A decimal code: Decimal numbers (0..9) are coded using 4-bit distinct binary words Observe that the codes 1010 .. 1111 (decimal 10..15) are NOT represented (invalid BCD codes) 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 35 Binary-Coded Decimal (cont.) To code a number with n decimal digits, we need 4n bits in BCD e.g. (365)10 = (0011 0110 0101)BCD This is different to converting to binary, which is (365)10 = (101101101)2 Clearly, BCD requires more bits. BUT, it is easier to understand/interpret 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 36 BCD Addition When 2 BCD codes are added: If the binary sum is less than 1010 (9 in decimal), the corresponding BCD sum digit is correct If the binary sum is equal or more than 1010, must add 0110 (6 in decimal) to the corresponding BCD sum digit in order to produce the correct carry into the digit to the left 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 37 BCD Addition (cont.) Example: Add 448 and 489 in BCD. 0100 0100 1000 (448 in BCD) 0100 1000 1001 (489 in BCD) 10001 (greater than 9, add 6) 1 0111 (carry 1 into middle digit) 1101 (greater than 9, add 6) 1001 1 0011 (carry 1 into leftmost digit) 1001 0011 0111 (BCD coding of 93710) 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 38 ASCII character code We also need to represent letters and other symbols alphanumeric codes ASCII = American Standard Code for Information Interchange. Also know as Western European It contains 128 characters: 94 printable ( 26 upper case and 26 lower case letters, 10 digits, 32 special symbols) 34 non-printable (for control functions) Uses 7-bit binary codes to represent each of the 128 characters 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 39 ASCII Table 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 40 Unicode Established standard (16-bit alphanumeric code) for international character sets Since is 16-bit, it has 65,536 codes Represented by 4 Hex digits ASCII is between 000016 .. 007B16 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 41 Unicode Table (first 191 char.) 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 42 ASCII Parity Bit Parity coding is used to detect errors in data communication and processing An 8th bit is added to the 7-bit ASCII code Even (Odd) parity: set the parity bit so as to make the # of 1’s in the 8-bit code even (odd) 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 43 ASCII Parity Bit (cont.) For example: Make the 7-bit code 1011011 an 8-bit even parity code 11011011 Make the 7-bit code 1011011 an 8-bit odd parity code 01011011 Both even and odd parity codes can detect an odd number of error. An even number of errors goes undetected. 9-Apr-15 Chapter 1: Digital Computers and Information PJF - 44 Summary Conversion between different number system Binary Arithmetic 9-Apr-15 Arithmetic PJF - 45