CPSC 11008: Computing on Mobile Phones

This lesson assumes you have completed the homework on binary and hexadecimal number systems. That homework decribed how the binary, decimal, and hexadecimal number systems work and showed how to convert from one number system to another.
In this lesson we want generalize what we learned their by seeing those number systems as specific examples of a more general concept, a positional number system.
We will develop algorithms that will enable you to perform conversions from one number system to another.
The type of generalization we are doing in this lesson is another example of the abstraction principle in computer science  here we are focusing on a general pattern that holds true for all positional number systems.
An algorithm
is a stepbystep procedure to perform some computation. For example,
the steps you take in the Hello Purr app when the button is
clicked is an example of a simple 2step algorithm:
To help us talk about algorithms we will use pseudocode, a language or notation that has many of the structures of a programming language but is easy to read. Pseudocdes are halfway between natural languages like English and formal programming languages.
Let's review some of the key points that you learned in the Khan Academy videos.
Name  Base  Symbols 

Decimal  10  0, 1, 2, 3, 4, 5, 6, 7, 8, 9 
Binary  2  0, 1 
Hexadecimal  16  0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F 
Octal  8  0, 1, 2, 3, 4, 5, 6, 7 
System  Base  Value  Conversion Formula  Decimal Value 

Decimal  10  104  (1 × 10^{2}) + (0 × 10^{1}) + (4 × 10^{0})  100 + 0 + 4 = 104 
Binary  2  111  (1 × 2^{2}) + (1 × 2^{1}) + (1 × 2^{0})  4 + 2 + 1 = 7 
Octal  8  104  (1 × 8^{2}) + (0 × 8^{1}) + (4 × 8^{0})  64 + 0 + 4 = 68 
Hexadecimal  16  FEC  (F × 16^{2}) + (E × 16^{1}) + (C × 8^{0})  15 × 256 + 14 × 16 + 12 × 1 = 3840 + 224 + 12 = 4076 
Let's summarize these conversion formulas by developing an general algorithm that will convert from any base into decimal.
Let's try it on the binary number 1011.
Let n = 4. Let b = 2. Let s = 0. First digit, 1: n = 3, 1 × b^{n} is 1 × 2^{3} = 8. So s = 8. Second digit, 0: n = 2, 0 × b^{n} is 0 × 2^{2} = 0. So s = 8. Third digit, 1: n = 1, 1 × b^{n} is 1 × 2^{1} = 2. So s = 10 Last digit, 1: n = 0, 1 × b^{n} is 1 × 2^{0} = 1. So 1011_{2} = 11_{10}
Digit n Value = Digit * b^{n} Running Total 1 3 1 × 2^{3} = 8 8 0 2 0 × 2^{2} = 0 8 1 1 1 × 2^{1} = 2 10 1 0 1 × 2^{0} = 1 11
Let's try it on the hex number 7E.
Let n = 2. Let b = 16. Let s = 0. First digit, 7: n = 1, 7 × b^{n} is 7 × 16^{1} = 7 × 16 = 112. So s = 112. Last digit, E: n = 0, 14 × b^{n} is 14 × 16^{0} = 14. So s = 112 + 14 = 126. So 7E_{16} = 126_{10}
Digit n Value = Digit * b^{n} Running Total 7 1 7 × 16^{1} = 112 114 E 0 14 × 16^{0} = 14 126
Let's try it on the octal number 124.
Let n = 3. Let b = 8. Let s = 0. First digit, 1: n = 2, 1 × b^{n} is 1 × 8^{2} = 1 × 64 = 64. So s = 64. Second digit, 2: n = 1, 2 × b^{n} is 2 × 8^{1} = 2 × 8 = 16. So s = 64 + 16 = 80. Last digit, 4: n = 0, 4 × b^{n} is 4 × 8^{0} = 4. So s = 80 + 4 = 84. So 124_{8} = 84_{10}
Digit n Value = Digit * b^{n} Running Total 1 2 1 × 8^{2} = 64 64 2 1 2 × 8^{1} = 16 80 4 0 4 × 8^{0} = 4 84
Let's use the algorithm to convert 45 into binary.
Let n = 45. Let b = 2. Repeat 45 divided by b is 45/2 = 22 remainder 1. So d=22 and r=1. So m= 1 and the new n is 22. 22 divided by b is 22/2 = 11 remainder 0. So d=11 and r=1. So m= 01 and the new n is 11. 11 divided by b is 11/2 = 5 remainder 1. So d=5 and r=1. So m= 101 and the new n is 5. 5 divided by b is 5/2 = 2 remainder 1. So d=2 and r=1. So m= 1101 and the new n is 2. 2 divided by b is 2/2 = 1 remainder 0. So d=1 and r=0. So m= 01101 and the new n is 1. 1 divided by b is 1/2 = 0 remainder 1. So d=0 and r=1. So m=101101 and the new n is 0. So 45_{10} = 101101_{2}
Let's use it to convert 99 into binary.
Let n = 99. Let b = 2. Repeat 99 divided by b is 99/2 = 49 remainder 1. So d=49 and r=1. So m= 1 and the new n is 49. 49 divided by b is 49/2 = 24 remainder 1. So d=24 and r=1. So m= 11 and the new n is 24. 24 divided by b is 24/2 = 12 remainder 0. So d=12 and r=0. So m= 011 and the new n is 12. 12 divided by b is 12/2 = 6 remainder 0. So d=6 and r=0. So m= 0011 and the new n is 6. 6 divided by b is 6/2 = 3 remainder 0. So d=3 and r=0. So m= 00011 and the new n is 3. 3 divided by b is 3/2 = 1 remainder 1. So d=1 and r=1. So m= 100011 and the new n is 1. 1 divided by b is 1/2 = 0 remainder 1. So d=0 and r=1. So m=1100011 and the new n is 0. So 99_{10} = 1100011_{2}
Let's use it to convert 45 into hexadecimal.
Let n = 45. Let b = 16. Repeat 45 divided by b is 45/16 = 2 remainder 13. So d=2 and r=13. So m= D and the new n is 2. 2 divided by b is 2/16 = 0 remainder 2. So d=0 and r=2. So m=2D and the new n is 0. So 45_{10} = 2D_{16}.
Let's use it to convert 99 into hexadecimal.
Let n = 99. Let b = 16. Repeat 99 divided by b is 99/16 = 6 remainder 3. So d=6 and r=3. So m= 3 and the new n is 6. 6 divided by b is 6/16 = 0 remainder 6. So d=0 and r=6. So m=63 and the new n is 0. So 99_{10} is 63_{16}.