樹德科技大學資訊工程系 Chapter 2: Number Systems Operations and Codes Shi-Huang Chen Sept. 2010 1 Chapter Outline 2.1 Decimal Numbers 2.2 Binary Numbers 2.3 Decimal-to-Binary Conversion 2.4 Binary Arithmetic 2.5 1's and 2's Complements of Binary Numbers 2.6 Signed Numbers 2.7 Arithmetic Operations with Signed Numbers 2.8 Hexadecimal Numbers 2.9 Octal Numbers 2.10 Binary Coded Decimal (BCD) 2.11 Digital Codes and Parity 2 1
Decimal Numbers -1 The decimal number system has ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9 The decimal numbering system has a base of 10 with each position weighted by a factor of 10.10 5 10 4 10 3 10 2 10 1 10 0. 10-1 10-2 10-3 10-4 10-5 14.2 = 1 x 10 1 + 4 x 10 0 + 2 x 10-1 Decimal Point 3 Decimal Numbers -2 3 X 10 1 + 7 X 10 0 is equal to 3.7 37 10 370 4 2
Decimal Numbers -3 The number 9240 can be expressed as or (9 x 10 3 ) + (2 x 10 2 ) + (4 x 10 1 ) + (0 x 10 0 ) 9 x 1,000 + 2 x 100 + 4 x 10 + 0 x 1 Express the number 480.52 as the sum of values of each digit. 480.52 = (4 x 10 2 ) + (8 x 10 1 ) + (0 x 10 0 ) + (5 x 10-1 ) +(2 x 10-2 ) 5 Binary Numbers -1 The binary numbering system has 2 digits: 0 and 1 The binary numbering system has a base of 2 with each position weighted by a factor of 2.2 5 2 4 2 3 2 2 2 1 2 0. 2-1 2-2 2-3 2-4 2-5 Decimal Point 10111 = 1 x 2 4 +0 x 2 3 +1 x 2 2 +1 x 2 1 + 1 x 2 0 =23 10 6 3
Binary Numbers -2 4 = log2 16 7 Binary Numbers -3 8 4
Binary Numbers -4 Illustration of a simple binary counting application. 9 Binary Numbers -5 The binary number 1110 is equal to the decimal number 3 1 7 14 10 5
Binary Numbers -6 The binary number 11001110 is equal to the decimal number 12 206 127 66 11 Binary Numbers -7 Convert the binary number 100101.01 to decimal. Start by writing the column weights; then add the weights that correspond to each 1 in the number. 2 5 2 4 2 3 2 2 2 1 2 0. 2-1 2-2 32 16 8 4 2 1. ½ ¼ 1 0 0 1 0 1. 0 1 32 +4 +1 +¼ = 37¼ 12 6
Decimal-to-Binary Conversion -1 Sum-of-weight method Binary weights 256 128 64 32 16 8 4 2 1 357 = 256 + 64 + 32 + 4 + 1 101100101 Binary weights 1024 512 256 128 64 32 16 8 4 2 1 1937 = 1024 + 512 + 256 + 128 + 16 + 1 11110010001 13 Decimal-to-Binary Conversion -2 Repeated division-by-2 method 14 7
Decimal-to-Binary Conversion -3 Converting Decimal fractions to Binary Using Sum-of-weights Binary weights 64 32 16 8 4 2 1.5.25.125.0625 95.6875 = 64 + 16 + 8 + 4 + 2 + 1 +.5 +.125 +.0625 1011111.1011 Repeated division by 2 yields the whole number while repeated multiplication by 2 of the fraction yields the binary fraction 15 Decimal-to-Binary Conversion -4 The decimal number 18 is equal to the binary number 11110 10001 10010 1111000 16 8
Decimal-to-Binary Conversion -5 The decimal number 188 is equal to the binary number 10111100 0111000 1100011 1111000 17 Decimal-to-Binary Conversion -6 Convert the decimal fraction 0.188 to binary by repeatedly multiplying the fractional results by 2. 0.188 x 2 = 0.376 carry = 0 MSB 0.376 x 2 = 0.752 carry = 0 0.752 x 2 = 1.504 carry = 1 0.504 x 2 = 1.008 carry = 1 0.008 x 2 = 0.016 carry = 0 Answer =.00110 (for five significant digits) 18 9
Binary Addition -1 0 + 0 = 0 Sum of 0 with a carry of 0 0 + 1 = 1 Sum of 1 with a carry of 0 1 + 0 = 1 Sum of 1 with a carry of 0 1 + 1 = 10 Sum of 0 with a carry of 1 11001 +1101 100110 25 +13 38 19 Binary Addition -2 The sum of 11101 + 10111 equals 110011 100001 110100 100100 20 10
Binary Subtraction -1 0-0 = 0 1-1 = 0 1-0 = 1 10-1 = 1 0-1 with a borrow of 1 1011-111 100 11-7 4 21 Binary Subtraction -2 The difference of 111 001 equals 100 111 001 110 22 11
Binary Multiplication 0 x 0 = 0 0 x 1 = 0 1 x 0 = 0 100110 1 x 1 = 1 x 101 100110 000000 38 x 5 190 100110 10111110 23 Binary Division Use the same procedure as decimal division 24 12
1's Complements of Binary Numbers -1 Example of inverters used to obtain the 1 s complement of a binary number 25 1's Complements of Binary Numbers -2 The 1's complement of 10011101 is 01100010 10011110 01100001 01100011 26 13
2's Complements of Binary Numbers -1 Example of obtaining the 2 s complement of a negative binary number. 27 2's Complements of Binary Numbers -2 The 2's complement of 11100111 is 11100110 00011001 00011000 00011010 28 14
Signed Numbers -1 Sign-Magnitude Form The left-most bit in a signed binary number is the sign bit A 0 is for positive, and a 1 is for negative Ex: (8-bit) +25 = 0 0011001 Sign bit Magnitude bits - 25 = 1 0011001 29 Signed Numbers -2 Sign-Magnitude Form Express the decimal number 37 as an 8-bit number in sign-magnitude. 10100101 00100101 11011000 11010001 30 15
Signed Numbers -3 1's Complement Form Ex: (8-bit) +25 = 00011001-25 = 11100110 (1's complement of 25) In the 1's Complement form, a negative number is the 1's complement of the corresponding positive number 31 Signed Numbers -4 1's Complement Form Determine the decimal values of the signed binary number 11110100 in 1's complement 116 12 11 128 32 16
Signed Numbers -5 2's Complement Form Ex: (8-bit) +25 = 00011001-25 = 11100111 (2's complement of 25) In the 2's Complement form, a negative number is the 2's complement of the corresponding positive number 33 Signed Numbers -6 Floating-Point Numbers (1/2) 32 bits S Exponent (E) Mantissa (Fraction, F) 1 bit 8 bits 23 bits Biased Exp (+127) 3.248 x 10 4 = 32480 = 111111011100000 2 1.11111011100000 x 2 14 14 + 127 = 141 = 10001101 2 0 10001101 1111101110000000000000 34 17
Signed Numbers -7 Floating-Point Numbers (2/2) Number = (-1) S (1+F)(2 E-127 ) S E F 1 10010001 10001110001000000000000 Number = (-1) 1 (1.10001110001)(2 145 127 ) = (-1)(1.10001110001)(2 18 ) = 1100011100010000000 2 = 407,688 10 35 Arithmetic Operations with Signed Numbers -1 Addition Both numbers positive Positive number > Negative number Positive number < Negative number Both numbers negative Overflow condition Negative number is presented in 2 s complement form. 36 18
Arithmetic Operations with Signed Numbers -2 Subtraction Take the 2 s complement of the subtrahend and add. Ex: 8 3 = 8 + ( 3) 12 ( 9) = 12 + 9 25 19 = 25 + ( 19) 120 ( 30) = 120 + 30 37 Arithmetic Operations with Signed Numbers -3 Multiplication Is equivalent to adding a number to itself a number of times equal to the multiplier Ex: 8x3 = 8+8+8 38 19
Arithmetic Operations with Signed Numbers -4 Division Dividend Divisor = Quotient Quotient is the number of times that the divisor will go into the dividend. Ex: 21 7 = 3 21 7 = 14 14 7 = 7 3 times 7 7 = 0 39 Hexadecimal Numbers -1 40 20
Hexadecimal Numbers -2 Counting in Hex. 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 1A, 1B, 1C, 1D, 1E, 1F, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 2A, 2B, 2C,.. Bin.-to-Hex. Conversion Hex.-to-Bin. Conversion Hex.-to-Dec. Conversion Dec.-to-Hex. Conversion 41 Hexadecimal Numbers -3 Hex. Addition 58 16 + 22 16 = 7A 16 8 16 + 2 16 = 8 10 + 2 10 = A 16 5 16 + 2 16 = 5 10 + 2 10 = 7 16 DF 16 + AC 16 = 18B 16 F 16 + C 16 = 15 10 + 12 10 = 27 10 27 10-16 10 = 11 10 = B 16 with a 1 carry D 16 + A 16 + 1 16 = 13 10 + 10 10 + 1 10 = 24 10 24 10-16 10 = 8 10 = 8 16 with a 1 carry 42 21
Hexadecimal Numbers -4 Hex. Subtraction (1/3) Getting the 2 s complement of a hexadecimal number, Method 1. 43 Hexadecimal Numbers -5 Hex. Subtraction (2/3) Getting the 2 s complement of a hexadecimal number, Method 2. 44 22
Hexadecimal Numbers -6 Hex. Subtraction (3/3) Getting the 2 s complement of a hexadecimal number, Method 3. 45 Octal Numbers -1 Counting in Oct. 0, 1, 2, 3, 4, 5, 6, 7 10, 11, 12, 13, 14, 15, 16, 17, 20, 21,.. Oct.-to-Dec. Conversion Dec.-to-Oct. Conversion 46 23
Octal Numbers -2 Oct.-to-Bin. Conversion Bin.-to-Oct. Conversion 47 Binary Coded Decimal (BCD) The 8421 code BCD Addition 48 24
Digital Codes and Parity -1 The Gray code 49 Digital Codes and Parity -2 Bin.-to-Gray Code 1 + 0 + 1 + 1 + 0 1 1 1 0 1 Bin. Gray Gray-to-Bin. Code 1 1 0 1 1 + + + + Gray 1 0 0 1 0 Bin. 50 25
Digital Codes and Parity -3 A simplified illustration of how the Gray code solves the error problem in shaft position encoders. 51 Digital Codes and Parity -4 ASCII code 52 26
Digital Codes and Parity -5 Parity Method for Error Detection 53 27