Presentation is loading. Please wait.

Presentation is loading. Please wait.

CPSC 217 T03 Week III Part #1: Base Conversion Hubert (Sathaporn) Hu.

Similar presentations


Presentation on theme: "CPSC 217 T03 Week III Part #1: Base Conversion Hubert (Sathaporn) Hu."— Presentation transcript:

1 CPSC 217 T03 Week III Part #1: Base Conversion Hubert (Sathaporn) Hu

2 Today’s Tutorial Tips Anatomy of a Number General Idea for Conversion Base Conversion  Converting into a Decimal  Converting from a Decimal  Converting a Non-Decimal into a Non-Decimal

3 News I won’t be doing any slide on Wednesday. Your prof wants you to cram on A1. However, I’ll be available for help. And don’t come to school on next Monday!

4 TIPS! 1.Wildcard (*) can be helpful when you are using Terminal.  Delete all files: rm *  Delete all PNG file: rm *.png  Delete all PNG file starts with pic: rm pic*.png 2.Be careful of the space in your Python program. A poorly aligned code won’t compile. 3.Want to know more what a Python function does? Use help(func_name) in the interactive shell.

5 Converting into a Decimal “I know that math is difficult,” Jim Prentice, a former Alberta premier

6 Converting into a Decimal A number of any base can be decomposed into this formula:  x = d 0 B 0 + d 1 B 1 + d 2 B 2 + … + d n B n If we have a decimal number 512, we can decompose it into:  2(10 0 ) + 1(10 1 ) + 5(10 2 ) This will be important for conversion.

7 Converting into a Decimal When we want to convert a non-decimal number into decimal, we decompose it. For example, we have a base 3 number: 210.  We first turn it into: 0(3 0 ) + 1(3 1 ) + 2(3 2 )  And we get: 21 That’s it. EXERCISE #1: Try converting a base 5 number 42 into a decimal number.

8 Converting into a Decimal Sometimes, we may have a base that is above decimal such as hexadecimal. In such case, there would be more than 10 symbols that can occupy a digit!  For instance, 15 would occupy a single digit in hexadecimal. We start to use A, B, C, D and so on to represent any digit that is above 9.  So 15 becomes F. EXERCISE #2: Convert BEEF, a hexadecimal number into a decimal one.

9 Converting from a Decimal Converting from a decimal number is trickier than converting to a decimal. However, we can still exploit the fact that a number can be decomposed into the form:  x = d 0 B 0 + d 1 B 1 + d 2 B 2 + … + d n B n

10 Converting from a Decimal So when we convert a decimal x into base B, we try to n such that:  B n ≤ x ≤ B n+1 In layman’s term, just pick a number n such that x is not that much larger than B n. So if we have a decimal number of 65 and we want to convert it into a Base 4 number, we choose n to be 3.

11 Converting from a Decimal Then, we divide x with B n and we note both the result and the remainder.  65 / 4 3 = 1 with remainder = 1 From this, we know that the leftmost digit is 1. We move on to the next digit.  We have 1. Set x to be the remainder and set n = n -1. And we keep repeat these steps until n = 0. At the same time, we also record the digits.

12 Converting from a Decimal  So the remainder was 1.  Then, we set x = 1 and n = 2.  1 / 4 2 = 0 with remainder = 1.  So our number becomes 10.  Then, we set x = 1 and n = 1.  1 / 4 1 = 0 with remainder = 1.  So the number becomes 100.  Then, we set x = 1 and n = 0.  1 / 4 1 = 1 with remainder = 0.  So the number is 1001 and that’s our answer!

13 Converting from a Decimal If you are converting into a number based higher than 10, then you can get a number higher than 10 during division as the answer. Replace that number with A, B, C, D, … EXERCISE #3: Convert 256 into a binary number. EXERCISE #4: Convert 256 into a Base 17 number.

14 Convert a Non-Decimal to a Non- Decimal EXERCISE #5: How can we convert a non- decimal into another non-decimal? EXERCISE #6: Convert a hexadecimal number FAD into a binary number.

15 Solutions Exercise #1: 22 Exercise #2: 48879 Exercise #3: 100000000 Exercise #4: F1 Exercise #5: We convert a number into a decimal first and then the decimal into our target base. Exercise #6: 111110101101


Download ppt "CPSC 217 T03 Week III Part #1: Base Conversion Hubert (Sathaporn) Hu."

Similar presentations


Ads by Google