“Magic Numbers” Approach to Introducing Binary Number Representation in CS0 Gerald Kruse Juniata College Huntingdon, PA USA kruse@juniata.edu Categories and Subject Descriptors K.3.2 [Computer and Information Science Education]: Computer science education. 1. INTRODUCTION This is a playful introduction to binary numbers, which can be used in an “Introduction to Computer Science” or CS0 course. While there is no formal assessment of this demonstration’s effectiveness, students seem to understand binary numbers well and are comfortable applying this knowledge when converting them to other bases. In addition, the interaction and increase in energy level resulting from the demonstration translates to a fruitful follow-up discussion. 9 10 11 our familiar decimal system, with 10 digits means we start “rolling over,” like a car odometer after the last digit is used. I end up writing from 0 to 15 on the board, as in Figure 1 (a). Then I ask, “What happens if we only have two digits to count with?” The answer is that we start “rolling over” much sooner. I write from 0 to 15 in binary on the board, fairly close to the base 10 count, as in Figure 1 (b). Next, I pad with 0’s, so each binary 0 1 10 11 100 101 110 (a) (b) 111 1000 1001 10 1010 11 1011 12 1100 13 1101 14 1110 15 1111 Figure 1. Decimal and Binary numbers. Copyright is held by the author/owner(s). . ITiCSE’03, June 30–July 2, 2003, Thessaloniki, Greece 4 5 6 7 (b) 12 13 14 15 3 6 12 13 14 15 7 (c) I begin by asking the students to help me count. I point out that ACM 1-58813-672-2/03/0006. 8 (a) 2 2. THE TECHNIQUE 0 1 2 3 4 5 6 7 8 9 number is represented with 4 bits. Next, I ask for a student volunteer. They come up to the board, write a number between 1 and 15 so everyone in the room can see it, except me, then erase the number and return to their seat. Then I show each of the four cards in Figure 2, asking the class to say, “Yes” when their number appears on a card, or “No” when it does not appear. We might iterate through this several times. As we do, I will remind 1 3 5 7 (d) 10 11 14 15 9 11 13 15 Figure 2. Four cards used to determine the number. the students that binary (digital) computers have circuits with just two states: ON (or 0) and OFF (or .1), so it is natural to use binary numbers to model digital circuits. I will also point out that their “Yes” or “No” responses can be considered as binary digits. Finally, if the suspense has become unbearable because no one has guessed how the demonstration works, I’ll take the most recent number written on the board and work through it. For example, if the number was 9, I successively produce each card. Using the labels in Figure 2, we see that 9 prompts a “Yes” to card (a) in Figure 2, “No” to card (b), “No” to card (c), and “Yes” to card (d). I tell the class that whenever they said, “Yes,” I added the smallest number on the card. So 9 = Yes, No, No, Yes = 1001 = 8 + 0 + 0 + 1 . Finally, I point out that each number on card (a) has a 1 in the fourth column (from the right) in its binary representation, each number on card (b) has a 1 in the third column in its binary representation, each number on card (c) has a 1 in the second column in its binary representation, and each number on card (d) has a 1 in the rightmost column in its binary representation. ACKNOWLEDGMENTS Our thanks to Jennifer Davenport, of Denver, Colorado, for introducing us to the magic trick that this tip is based on.