CS107 Spring 2004 Converting from decimal to base n Idea: find the largest power of n that can represent this number April 2, 2004 More fun with number systems Binary arithmetic Procedure: Examples 100810 = 130135 Check: 1*54 + 3*53 + 1*5 + 3 = 1008 1/5 = 0 rem 1 replace number by quotient repeat until quotient is 0 Convert 496 to base 8: 8/5 = 1 rem 3 40/5 = 8 rem 0 201/5 = 40 rem 1 remainder = next leftmost digit Examples (cont.) 1008/5 = 201 rem 3 Convert 1008 to base 5: divide number by n 496/8 = 62 rem 0 62/8 = 7 rem 6 7/8 = 0 rem 7 49610 = 7608 Check: 7*64 + 6*8 = 448 + 48 = 496 convert from base n to decimal convert from decimal to base m Octal to binary: Replace each digit with its 3digit binary equivalent Example: Convert 332 from base 5 to base 3 3325 = 92, 92 = 101023 4568 = 302, 302 = 100101110 10101001 = 169, 169 = A9 3616 to binary: 0011 0110 101010101 to hex: 1 0101 0101 = 15516 11011010 to hex: 1101 1010 = DC16 368 = 011 110 101010100 = 101 010 100 = 5248 10101010101010 = 10 101 010 101 010 = 252528 Same procedure as decimal arithmetic: 4AC16 to binary: 0100 1000 1010 175348 = 001 111 101 011 100 Arithmetic in other bases Converting from hex to binary, and vice versa Same procedure as octal, only use groups of 4 digits 4568 = 100 101 110 Binary to octal: Group into 3 digits, then convert each group to octal Example: Convert 10101001 to hex Example: Convert 4568 to binary Converting from octal to binary, and vice versa Procedure: Converting from base n to base m add up digits in the same position in both numbers if the sum is less than n-1, digit in position of new number = sum otherwise, digit in position of new number = n – sum, and add 1 to the two digits in the next position over to the left 3325 + 2045 = 10415 check: 458 + 1038 = 37 + 67 = 104 = 1508 10101010 + 10011010 = 101000100 check: 3325 + 2045 = 92 + 54 = 146 = 10415 458 + 1038 = 1508 Examples: check: 170 + 154 = 324 = 101000100 CS107 Spring 2004 More fun with number systems Binary arithmetic April 2, 2004 Idea: find the largest power of n that can represent this number Procedure: Converting from decimal to base n divide number by n remainder = next leftmost digit replace number by quotient repeat until quotient is 0 Examples Convert 1008 to base 5: 1008/5 = 201 rem 3 201/5 = 40 rem 1 40/5 = 8 rem 0 8/5 = 1 rem 3 1/5 = 0 rem 1 100810 = 130135 Check: 1*54 + 3*53 + 1*5 + 3 = 1008 Examples (cont.) Convert 496 to base 8: 496/8 = 62 rem 0 62/8 = 7 rem 6 7/8 = 0 rem 7 49610 = 7608 Check: 7*64 + 6*8 = 448 + 48 = 496 Converting from base n to base m Procedure: convert from base n to decimal Example: Convert 332 from base 5 to base 3 4568 = 302, 302 = 100101110 Example: Convert 10101001 to hex 3325 = 92, 92 = 101023 Example: Convert 4568 to binary convert from decimal to base m 10101001 = 169, 169 = A9 Octal to binary: Replace each digit with its 3digit binary equivalent 4568 = 100 101 110 175348 = 001 111 101 011 100 368 = 011 110 Binary to octal: Group into 3 digits, then convert each group to octal Converting from octal to binary, and vice versa 101010100 = 101 010 100 = 5248 10101010101010 = 10 101 010 101 010 = 252528 Same procedure as octal, only use groups of 4 digits Converting from hex to binary, and vice versa 4AC16 to binary: 0100 1000 1010 3616 to binary: 0011 0110 101010101 to hex: 1 0101 0101 = 15516 11011010 to hex: 1101 1010 = DC16 Arithmetic in other bases # " ! Same procedure as decimal arithmetic: add up digits in the same position in both numbers if the sum is less than n-1, digit in position of new number = sum otherwise, digit in position of new number = n – sum, and add 1 to the two digits in the next position over to the left 3325 + 2045 = 10415 check: 458 + 1038 = 37 + 67 = 104 = 1508 10101010 + 10011010 = 101000100 ) ( check: 3325 + 2045 = 92 + 54 = 146 = 10415 458 + 1038 = 1508 ' & % $ Examples: check: 170 + 154 = 324 = 101000100