Beyond Binary

Here's our solution:

decimal->binary

The base case is that the number fits in a single bit—that is, it has to be less than 2. If so, the number itself, 0 or 1, is the desired output.

In the recursive case, the rightmost bit of the result is the remainder of dividing the number by 2. That is, even numbers end with 0, and odd numbers end with 1. The rest of the result is a recursive call on the (integer) quotient of the number divided by 2. The combiner is join because we want to string the digits together. It may be surprising that we don't use an arithmetic operator, since we're working with numbers, but the desired result is a numeral, which is a visible representation of a number, rather than the numeric value itself. A numeral is a text string, so the combiner is a string operation.

Other Bases

There's no reason to limit ourselves to decimal (base 10) and binary (base 2). How about base 7?

base7 of 9827 reports 40436

The base 7 digits are 0‒6, and the digit positions represent powers of 7.

  1. The first few powers of 7 are 1, 7, 49, 343, 2401, and 16807. Grab a calculator and check that our base7 block above got the right answer.
  2. Write the base7 block.
  3. Generalize the pattern with a base block that takes the base as a second input:

    9827 base 29827 base 7
    9827 base 89827 base 10

  1. Improve the base block so that it can go up to base 36 by using the letters az as digits with values 10‒35. 32562 base 1553749006 base 36

Another thing the AP people want you to know is that using a power of two as the base gives a shorthand for the binary representation — each digit represents a certain number of bits. Commonly used bases are octal (base 8), in which each digit represents three bits, and hexadecimal (base 16), in which each digit represents four bits.

  1. What do we mean by "represents" above? Why does this work? Discuss with other students and make sure you all have the same idea. (What are the first few powers of 8?)
  2. Write the inverse function from base that takes a (text) string of digits and a base as inputs, and reports the corresponding number (which Snap! will show in decimal, of course, but you're converting to a number, not to a string of digits). Again, bases above ten are optional.
    (23143) from base (8)
A useful thing to remember during the AP test is that 210=1024≈103. So a 3- or 4-digit number in decimal will be around 10 bits; a 6- or 7-digit number will be about 20 bits, and so on. This can help eliminate multiple-guess answers that are wildly off.
Long ago, when computer circuits were very unreliable, some computers used a representation called biquinary, which used decimal digits, each of which was represented using five bits, of which exactly two had to be 1, and the other three had to be 0. The five bits represented the values 0, 1, 2, 4, and 7. (Yes, that's a strange set of bit values. Bear with us.)
GH feedback 8/2/15: I don't understand the question being asked in "Take It Further" #6. It's unclear. (It's letter B, now. --MF)
  1. Check that each of the values from 1 to 9 can be represented as the sum of two of those bit values in exactly one way.
  2. There's exactly one more possible set of two of those five bit values; that one represents zero. What are those two bits?
  3. How does this help solve the problem of unreliable circuits?