At My Fingertips

The CS Unplugged collection includes a great lesson introducing binary numbers.

Encoding Numbers

Assume you want to represent numbers with cards. Here we have 8 cards. You see the front side of the rightmost card, and the back sides of the seven other cards.

1

You see one dot (on the rightmost card). Because you see one dot, this configuration represents the number 1.

Here is how you would represent the number 2:

2

The card with its front-side visible contains two dots.

The answer is 4. Really?? Yes!

It contains 8 dots.

Here are all our eight cards with their front side showing:

255

Yes! You double the number! Each card has twice as many dots as its right neighbor. Each card represents a power of two:

  • 20 = 1
  • 21 = 2
  • 22 = 4
  • 23 = 8

Representing Arbitrary Numbers

We know how to represent the numbers 1, 2, 4, 8, ... (the powers of two). But how to represent other natural numbers?

You show the dots of two of the cards: the card with 1 dot, and the card with 2 dots:

3

Here is how:

10

This scheme allows you to represent numbers also between the powers of two.

Bits and Bytes

Each card represents a bit. A bit can be on (dots showing) or off (dots hidden). Eight bits correspond to a byte. So our eight cards represent one byte.

Maximum and Minimum

You would show the dots on all cards:

255

You see a total of 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 dots.

Thus, the largest number you can represent with eight bits is 255.

Yes, the smallest number is 0. You can flip all cards over so none of them shows its dots.

0

Similarities and Differences

Now let's write a program that can generate these cards.

Each graphic seems to consist of a white sheet. The square area at the top of the sheet contains the dots, the band at the bottom of the sheet contains a decimal number representing the dot count.

The number of dots is always a power of 2.

The aspect ratio of the sheet is 1 : sqrt(2).

The cards differ in the number of dots they represent.

If the number of dots is a power of 4 (e.g., 1, 4, 16, ...), then the dots are arranged into a square. Otherwise the dots are arranged into a rectangle, with twice as many rows than columns.

Implementation

Hint: Divide the square into smaller squares. You may want to do that recursively. Place the dots inside the smallest squares. The diameter of each dot is 80% of side of the smallest square.

Loading...

This activity has been created by LuCE Research Lab and is licensed under CC BY-SA 4.0.

Binary Numbers

Logo of PyTamaro

PyTamaro is a project created by the Lugano Computing Education Research Lab at the Software Institute of USI

Privacy PolicyPlatform Version b744b47 (Tue, 08 Oct 2024 16:30:14 GMT)