Number Conversion Microprocessors & Interfacing • From base r to base 10 – Using (a a ...a a . a ...a ) Number Conversion n n 1 1 0 - 1 - m r − − − − n n 1 1 m a r a r − − − − ... a r a a r − − − − ... a r − − − − = = = = × × × × + + + + × × × × + + + + + + + + × × × × + + + + + + + + × × × × + + + + + + + + × × × × n n 1 1 0 - 1 m − − − − − − − − • Examples: Lecturer : Dr. Annie Guo S2, 2008 COMP9032 Week1 1 S2, 2008 COMP9032 Week1 2 Examples Number Conversion • From base 2 • From base 10 to base r Based on the formula 3 2 -1 (1011.1 ) 1 2 0 2 1 2 1 1 2 11.5 = = = = × × × × + + + + × × × × + + + + × × × × + + + + + + + + × × × × = = = = 2 (a a ...a a . a ...a ) • From base 16 n n 1 1 0 - 1 - m r − − − − n n 1 1 m a r a r − − − − ... a r a a r − − − − ... a r − − − − = = = = × × × × + + + + × × × × + + + + + + + + × × × × + + + + + + + + × × × × + + + + + + + + × × × × n n 1 1 0 - 1 m − − − − − − − − 2 (10A) 1 16 0 16 10 266 = = = = × × × × + + + + × × × × + + + + = = = = – For whole number 16 • Divide the number/quotient repeatedly by r until the quotient is zero and the remainders are the digits of base r number, in reverse order – For fraction • Multiply the number/fraction repeatedly by r , the whole numbers of the products are the digits of the base r fraction number S2, 2008 COMP9032 Week1 3 S2, 2008 COMP9032 Week1 4
Examples Examples • To base 2 • To base 16 – To convert (11.25) 10 to binary – To convert (99.25) 10 to hexadecimal • For whole number (11) 10 – division (by 2) • For whole number (99) 10 – division (by 16) 11 1 99 3 5 1 6 6 2 0 0 1 1 • For fraction (0.25) 10 – multiplication (by 16) 0 0.25 • For fraction (0.25) 10 – multiplication (by 2) 0.0 4 0.25 0.5 0 0.0 1 (99.25) 10 =(63.4) hex (11.25) 10 =(1011.01) 2 S2, 2008 COMP9032 Week1 5 S2, 2008 COMP9032 Week1 6 Number Conversion Number Conversion • Between binary and octal • Between binary and octal (cont.) – Direct mapping based on the observation: – Binary to octal • The binary digits (“bits”) are grouped from the radix point, three (abcdefgh. jklmn) digits a group. Each group corresponds to an octal digit. 2 6 2 3 (a 2 b) 2 (c 2 d 2 e) 2 = = ⋅ ⋅ + + ⋅ ⋅ + + ⋅ ⋅ + + ⋅ ⋅ + + ⋅ ⋅ + + – Octal to binary = = ⋅ ⋅ + + ⋅ ⋅ + + ⋅ ⋅ + + ⋅ ⋅ + + ⋅ ⋅ + + 2 2 3 (f 2 g 2 h) (j 2 k 2 l) 2 − − − − ⋅ ⋅ ⋅ ⋅ + + + + ⋅ ⋅ ⋅ ⋅ + + + + + + + + ⋅ ⋅ ⋅ ⋅ + + + + ⋅ ⋅ ⋅ ⋅ + + + + ⋅ ⋅ ⋅ ⋅ + + + + • Each of octal digits is expanded to three binary digits 2 6 (m 2 n 2 0) 2 − − − − ⋅ ⋅ ⋅ ⋅ + + + + ⋅ ⋅ ⋅ ⋅ + + + + ⋅ ⋅ ⋅ ⋅ 2 1 0 (0ab ) 8 (cde ) 8 (fgh ) 8 = = = = ⋅ ⋅ ⋅ ⋅ + + + + ⋅ ⋅ ⋅ ⋅ + + + + ⋅ ⋅ ⋅ ⋅ + + + + 2 2 2 1 2 (jkl ) 8 − − − − (mn0 ) 8 − − − − ⋅ ⋅ ⋅ ⋅ + + + + ⋅ ⋅ ⋅ ⋅ 2 2 – The expressions in parentheses, being less than 8, are the octal digits. S2, 2008 COMP9032 Week1 7 S2, 2008 COMP9032 Week1 8
Examples Examples • Binary to octal • Octal to binary – Convert (10101100011010001000.10001) 2 to octal : – Convert 37425.62 8 to binary : 010 101 100 011 010 001 000 . 100 010 2 3 7 4 2 5 . 6 2 8 = 2 5 4 3 2 1 0 . 4 2 8 = 011 111 100 010 101 . 110 010 2 = 2543210.42 8 . = 11111100010101.11001 2 • Note: • Note: – Whole number parts are grouped from right to left. The – For whole number parts, the leading 0s can be omitted. leading 0 is optional – For fractional parts, the trailing 0s can be omitted. – Fractional parts are grouped from left to right and padded with 0s S2, 2008 COMP9032 Week1 9 S2, 2008 COMP9032 Week1 10 Number Conversion Examples • Between binary and hexadecimal • Binary to hexadecimal – Binary to hexadecimal – Convert 10101100011010001000.10001 2 to hexadecimal : • The binary digits (“bits”) are grouped from the radix point, four binary digits a group. Each group corresponds to a hexadecimal 1010 1100 0110 1000 1000 . 1000 1000 2 digit. = A C 6 8 8 . 8 8 16 – Hexadecimal to binary = AC688.88 16 . • Each of hexadecimal digits is expanded to four binary digits • Note: – Whole number parts are grouped from right to left. The leading 0 is optional – Fractional parts are grouped from left to right and padded with 0s S2, 2008 COMP9032 Week1 11 S2, 2008 COMP9032 Week1 12
Examples Conversion to binary via octal The direct conversion of ... and gives 11111010001. • Hexadecimal to binary 2001 10 to binary looks It may be quicker to like this ... – Convert 2F6A.78 16 to binary : convert to octal first ... 2001 2 F 6 A . 7 8 16 1000 1 2001 = 0010 1111 0110 1010 . 0111 1000 2 500 0 250 1 = 10111101101010.01111 2 250 0 31 2 125 0 • Note: 62 1 3 7 31 0 – For whole number parts, the leading 0s can be omitted. 0 3 15 1 – For fractional parts, the trailing 0s can be omitted. ... yielding 3721 8 , which 7 1 3 1 can be instantly converted 1 1 0 1 to 11 111 010 001 2 . S2, 2008 COMP9032 Week1 13 S2, 2008 COMP9032 Week1 14
Recommend
More recommend