Presentation is loading. Please wait.

Presentation is loading. Please wait.

PSU CS 106 Computing Fundamentals II Base Conversion of Integer Numbers HM 4/6/2008.

Similar presentations


Presentation on theme: "PSU CS 106 Computing Fundamentals II Base Conversion of Integer Numbers HM 4/6/2008."— Presentation transcript:

1 PSU CS 106 Computing Fundamentals II Base Conversion of Integer Numbers HM 4/6/2008

2 2 © Dr. Herbert G. Mayer Agenda General Conversion Algorithm Digits for Numbers with Base b Power-Series Conversion Decimal to Binary Samples Binary to Decimal Samples Simplest Conversions

3 3 © Dr. Herbert G. Mayer General Conversion Algorithm The general algorithm for converting a non-negative number N of base b = 10 to a number T of same value but different base t, with t being an integer Summary: –Repeatedly divide N by t and record the remainders modulo t –New number T is the sequence of the remainder digits in reverse order of creation Detail of Algorithm: –This method uses repeated integer division of N by t: –Starting with the absolute value of N, set the initial quotient q = N: –Step 1: perform the next integer division, create the quotient q = q / t, and record the remainder in the range 0.. t-1 –Step 2: Is q = 0? If so, go to step 4 –Step 3: Else repeat step 1 –Step 4: record the remainders from bottom-to-top in left-to-right order. That is the new number T

4 4 © Dr. Herbert G. Mayer General Conversion Algorithm Example: Convert decimal N = 500 to an equivalent number T of base t = 2: N base 10Quotient N / 10 repeated Remainder base 2 500 / 2 =2500 250 / 2 =1250 125 / 2 = 621 62 / 2 = 310 31 / 2 = 151 15 / 2 = 71 7 / 2 = 31 3 / 2 = 11 1 / 2 = 01 STOP Binary representation of decimal 500 is 111110100

5 5 © Dr. Herbert G. Mayer Digits for Numbers with Base b Digits for Numbers with base b are explained here: Any plausible numeric system using a power-series similar to the decimal system should have a base b > 1. Consequently there may be a need for new digits. In the decimal system we use the digits ‘0’.. ‘9’. If a base b > 10 is chosen, a typical convention is to use additional digits from the Roman alphabet, with digit ‘a’ standing for the value 10, ‘b’ for the value 11 etc. Interestingly, the Babylonian system with base 60 did not use that convention. Instead, this system created 60 different digits via a combination of distinct digits (like the 10 decimal digits) and repetition (like the Roman system, that lists 3 ‘X’ for decimal 30). In the hexadecimal system (base b = 16) the convention is to use letters ‘a’.. ‘f’ for the digits values 10 through 15. Lower- and upper-case letters are used synonymously for convenience.

6 6 © Dr. Herbert G. Mayer Digits for Numbers with Base b=16 Hexadecimal digitEquivalent decimal values 00 11 22 33 44 55 66 77 88 99 A10 B11 C12 D13 E14 F15

7 7 © Dr. Herbert G. Mayer Digits for Numbers with Base b=16 Example 1: –symbol 3ba8 is a possible hexadecimal number. The highest power of 16 used in 3ba8 is in the position associated with hex digit ‘3’. Its corresponding numeric value is 16 3, which is 4,096 in decimal. Thus hex digit ‘3’ contributes 3 * 4096 to the total value of the number. The ‘b’ contributes 11 * 16 2, etc. Example 2: –0A01_FFFF is a possible hexadecimal number. The highest power of 16 used in this hex number is the one in the position associated with digit ‘A’. Its corresponding numeric value is 16 6 = 16,777,216 in decimal. –Thus hex digit ‘A’ contributes 10 * 16,777,276 to the total value of number 0A01_FFFF. It could also be written as: a01fff. For numeric systems with bases b < 10 there is no need to invent new digits. A subset of the 10 decimal digits can be used.

8 8 © Dr. Herbert G. Mayer Digits for Numbers with Base b=20 Should we chose the base b = 20, we’d have to invent 4 more digits. Adding ‘g’ to ‘j’ to hex digits might be plausible, shown below. Base 20 digitsEquivalent decimal values 00 1.. 8 99 A10 B11 C12 D13 E14 F15 G16 H17 I18 J19

9 9 © Dr. Herbert G. Mayer Power-Series Conversion General power-series conversion of a number T with a base different from 10 to an equivalent number N with base b = 10. Summary: –Starting with an initial decimal value of 0, for all digits d of T, multiply d by the corresponding power of t, and add to the total decimal result. Detail: –Starting with an initial total value of 0, the initial exponent e = 0 for base t, and the rightmost digit d 0 of T, do the following: –Step 1: set the exponent e = 0, and the total decimal value total = 0 –Step 2: compute t e, multiply d e by t e and add to total –Step 3: increase e by 1 –Step 4: move to the next higher digit d if one is left, and execute step 2; else go to step 5 –Step 5: we are done, all digits have been taken into account, and the decimal value is in total.

10 10 © Dr. Herbert G. Mayer Power-Series Conversion Example for base 2: binary number 0100010 is to be converted to decimal: –Tracking the exponents e for the powers of 2 in 100010, there are only 2 contributing binary digits different from 0. –Ignore leading zeros; there is one leading 0 –These 1 digits are at exponents e=1 and e=5 –I.e. 1 * 2 1 which is 2, and 1 * 2 5 which is 32. –Hence the total is 2 + 32 = 34. Example for base 16: hexadecimal number 1ba0 is converted to base 10: –Tracking the exponents e for the powers of 16, we see, that there are only 3 contributing hex digits different from 0. –These are at powers 1, 2, and 3 a 16 * 16 1 = a 16 * 16 = 10 * 16 b 16 * 16 2 = b 16 * 256 = 11 * 256, and 1 16 * 16 3 = 1 16 * 4096 = 4096. –Hence the total = 10 * 16 + 11 * 256 + 1 * 4096 = 7072.

11 11 © Dr. Herbert G. Mayer Decimal to Binary Samples Decimal NQuotient N / 2Remainder 678 / 2 =3390 339 / 2 =1691 169 / 2 =841 84 / 2 =420 42 / 2 =210 21 / 2 =101 10 / 2 =50 5 / 2 =21 2 / 2 =10 1 / 2 =01 STOP Algorithmic base conversion works for target base > 10 and < 10. Binary representation of decimal 678 is 1010100110

12 12 © Dr. Herbert G. Mayer Decimal to Hexadecimal Samples N = 500 base 10Quotient N / 16 repeatedRemainder base 16 Written in base 10 and 16 500 / 16 =31 4 = 4 31 / 16 =115 = f 1 / 16 =0 1 = 1 STOP Hexadecimal representation of decimal 500 10 is 1f4 16

13 13 © Dr. Herbert G. Mayer Decimal to Hexadecimal Samples Hexadecimal representation of decimal 1,678 10 is 68e 16 N = 1678 base 10Quotient N / 16 repeatedRemainder base 16 1678 / 16 =104 14 = e 104 / 16 = 6 8 = 8 6 / 16 = 0 6 = 6 STOP

14 14 © Dr. Herbert G. Mayer Binary to Decimal Samples Binary number 10001011 converted to decimal, using power- series: –1 * 2 0 = 1 + –1 * 2 1 = 2 + –1 * 2 3 = 8 + –1 * 2 7 = 128 + Decimal representation binary 10001011 2 is 139 10 Binary number 100001010 converted to decimal, using power- series: –1 * 2 1 = 1 + –1 * 2 3 = 2 + –1 * 2 8 = 256 + Decimal representation binary 100001010 2 is 259 10

15 15 © Dr. Herbert G. Mayer Simplest Conversions Conversion from source base b s to target b t are trivial, whenever one is an integral power of the other This is the case between source base b s = 16 to target base b t = 2 Also between bases b s = 16 and b t = 4 Also between bases b s = 16 and b t = 8 Also between bases b s = 4 and b t = 2 Etc. It is easy to see that in such cases the conversion via numeric computation can be by-passed altogether Instead, the conversion amounts to simply re-writing the digits in expanded or contracted form For example, the hex number 1f 16 rewritten in binary is 11111 2

16 16 © Dr. Herbert G. Mayer Simplest Conversions Decimal valueBinary numberBase 4 equivalent 584100100100021020 409610000000000001000000 4_0961_00_00_00_00_00_001000000 Decimal valueBinary numberHexadecimal equivalent 5841001001000 = 10_0100_1000248 40961_0000_0000_0000 = 10000000000001000 Decimal valueHexadecimal numberBinary equivalent 65,535FFFF1111_1111_1111_1111 4,294,967,295FFFF_FFFF1111_1111_1111_1111_1111_1111_1111_1111 2,748ABC1010_1011_1100 5001F41_1111_0100


Download ppt "PSU CS 106 Computing Fundamentals II Base Conversion of Integer Numbers HM 4/6/2008."

Similar presentations


Ads by Google