Number Base Conversion Practice Solutions

This is a paper and pencil exercise. Show your calculations.

1. Complete the table. Show your calculation. Each column represents a base and each row should have the same number in it, represented by the various bases.

281016
1011110002741880xbc
10101010125850x55
111000000x3402240xE0
1111111101010776540850xFF5

Solution.

Let's do the top row

101111002 → 10 111 100
             2   7   4    →   0274
101111002  = 188

    1    0    1    1    1    1    0    0          

2        2    4   10   22   46   94  188
_________________________________________
    1    2    5   11   23   47   94  188

101111002  → 1011 1100
                b    c →   0xbc

Now for the second row.

0125 = 0    1    2     5
            1  010   101   → 1010101

10101012 = 101 0101
             5    5   → 0x55

0x55 = 5*15 + 5 = 85.

Now for row 3

224 % 16 = 0 = 0x0
224/16 = 14 = 0xD
224 = 0xD0.

To get binary:

0xD0 → 111000000

To get Octal

111000000 → 111 000 000
              7   0   0   → 0700

Finally the last row.

First decimal

      F       F         5
16          240      4080
_________________________________
      15    255      4085

2. Fill in the blanks. Each question mark stands for a single digit.

0x?4 = 124?5

Solution.

Let's give names to the missing digits like so

0xp4 = 124q5.

Converting, we know

0xp4 = 16p + 4

and that

124q5 = 125 + 50 + 20 + q.

Equating these we get

    16p + 4 = 195 + q

or 

    16p = 191 + q.  

By our definition of base, we know 0 ≤ p < 16 and 
0 ≤ q < 4, and that p and q must be integers.  
Let's try all possible values for q.

q = 0:
This gives 16p = 191.  No q works

q = 1:
This gives 16p = 192.  q  = 12 = 0xc works

q = 2:
This gives 16p = 193.  No q works

q = 3:
This gives 16p = 194.  No q works

q = 4:
This gives 16p = 195.  No q works

We get the solution

0xc4 = 12425

3. How can you use % to obtain the last digit of a number represented by the symbol x? The last two digits? What can you say in general?

Solution. If x is a number and b is the base, then x % b is the last digit. Here is a quick reason why.

Suppose we have a number in base b, ABCD.... Z. The last digit is in the base alphabet so 0 ≤ Z ≤ b. Any other term has place value of b or higher; each contributes a term in a sum divisible by b. Therefore,

ABC....Z % b = Z

4. A Balanced Ternary Expansion It is possible to uniquely write any integer in an expansion of the form

 e0 + 3*e1 + 32e2 + . . . + en*3n,

where each of the eks are 0, 1 or -1. Find such an expansion for each of the folllowing numbers: 5, 13, 37 and 79.

Solution.

5 = 9 - 3 - 1
13 = 9 + 3 + 1
37 = 27 + 9 + 1
79 = 81 - 3 + 1

5. Be an agent of Change Describe how to count out any amount between $0.00 and $1.00$ using standard coins to use as few coins as possible. You will find using // and % handy.

Solution.

Suppose you have x cents. We will proceed greedily. Let

q ← x//25
x ← x %25

This procedure uses as many quarters as possible. It then reassigns x to the amount left over after taking the quarters.

Now do this.

d ← x//10
x ← x%10

We now oink up the dimes and revise the amount. Repeat this for nickels and pennies and you will use q quarters, d dimes, n nickels, and p pennies.