Quarry School

Information in bits — quick review

Give one bit a meaning

In plain words

Two friends agree on a porch light ahead of time.

Rule

A bit has two possible states, so each mark on paper is either 0 or 1. A mark has no meaning until the sender and the receiver share a key, and that key must give the two marks two different meanings. Decode one mark at a time, from left to right, and write one word per mark. Keep the marks and change the key, and you get a different message.

Step

  1. Agree on the key with your partner and write it down before anyone sets a tile. In this lesson, Key A says 0 means waiting and 1 means ready.

  2. To reverse a key, swap its two meanings. Key B, the reversed lift key, says 0 means ready and 1 means waiting. Reversing the key never changes the order of the marks.

  3. Check the marks. Only 0 and 1 are allowed. If a mark is anything else, or a tile shows no clear face, write "not in the key" and stop instead of guessing.

  4. Start with an empty message. This is the starting point of your trace.

  5. Read the marks from left to right, one mark at a time.

  6. Look up each mark in the key and write its word at the end of the message. In this lesson a mark means the same thing wherever it sits in the row.

  7. Stop when no marks are left. Count the words: there should be one word for each mark.

  8. To encode, work the other way: look up each word in the key and write its mark.

  9. To check, encode your decoded message with the same key. If you get the original marks back, your decoding holds. This check is called a round trip.

Trap

The usual mistake is to treat the mark as if it were the meaning: "1 is ready, full stop." The tile shows only a face. Ready comes from Key A, and under Key B the same face means waiting. A computer keeps physical states, not the digits 0 and 1, so the digit names a state and the key turns that state into a meaning.

The second mistake is to reverse the marks instead of the key. The row 1, 0, 1 reads the same backwards, so flipping its order gives 1, 0, 1 again and repeats Key A's message without anyone noticing. Try 1, 1, 0 under Key A instead: it decodes as ready, ready, waiting.

Reversing the order of 1, 1, 0 gives 0, 1, 1, which Key A decodes as waiting, ready, ready. Reversing the key gives waiting, waiting, ready. These are three different answers, and only the last one answers "reverse the key".

A key that gives both marks the same meaning, such as 0 means ready and 1 means ready, also fails. Every message decodes as ready, and waiting can never be sent.

Read binary place values

In plain words

Picture a kitchen scale and four weights marked 8 grams, 4 grams, 2 grams and 1 gram.

Rule

This rule models one convention: a four-bit unsigned pattern with exactly four bits, columns worth 8, 4, 2 and 1 from left to right, and no sign. Other conventions read the leftmost bit as a plus or minus sign, so the answers here hold only when the pattern is stated to be unsigned.

To decode, the initial state is a total of 0 with your pencil at the 8 column. Move left to right, one column at a time. If the column holds 1, add its value to the total; if it holds 0, add nothing. Stop after the 1 column, and the total is the number.

To encode, the initial state is a remaining amount equal to the number, with four empty boxes under the headers 8 4 2 1. Move left to right, starting at the 8 column. If the column's value is no more than the remaining amount, write 1 and subtract the value; otherwise write 0. Stop after the 1 column. If the remaining amount is 0, the four marks are the pattern; if it is not 0, the number does not fit in four bits.

Step

  1. Write the headers 8 4 2 1 across your paper and draw a box under each.

  2. To decode, copy the pattern into the boxes, one digit per box. Check that there are four digits and that each is 0 or 1.

  3. Set the total to 0. Working left to right, add the header of every box that holds 1 and skip every box that holds 0.

  4. Stop after the 1 column. The total is your number.

  5. To encode, write the number as the remaining amount. Working left to right, ask whether the header is no more than the remaining amount.

  6. If it is, write 1 in that box and subtract the header from the remaining amount. If it is not, write 0.

  7. Stop after the 1 column. If the remaining amount is 0, read the four boxes as your pattern and keep any leading zeros. If it is not 0, the number does not fit in four bits.

  8. Check by running the rule the other way: decode the pattern you wrote, or encode the number you found, and compare with where you started.

Trap

If you read 1010 as one thousand and ten, you have used decimal columns, which step by ten: 1000, 100, 10 and 1. The same four marks then name a much larger number. Binary columns step by two, so 1010 in binary is 8 + 2 = 10.

Three habits guard against this. Write "in binary" next to the pattern, write the headers 8 4 2 1 above the digits before you read, and check that every digit is 0 or 1. A digit from 2 to 9 means the marks are not a binary pattern at all.

Find what fits in three bits

In plain words

Imagine a fair booth that prints 000 on its first ticket.

Rule

Start with one empty row and double the count for each bit you add. Two choices for each of three positions make eight patterns. The unsigned values run from zero to one less than the pattern count. With three bits they run from 0 through 7.

Step

  1. Write down the convention, the rule for using the codes. Give each ticket a different three-bit pattern and make every pattern available.

  2. Begin with no digits and fill the slots from left to right. Follow 0 before 1 at each split. Record the row once it has three digits, then go back to the last split with a branch left to follow. Finish when you have followed every branch.

  3. Count the finished rows, including 000. Find the largest unsigned value by adding the place values in the all-ones row.

Trap

If you counted seven codes, check where you started. The pattern 000 reads as zero and is one of the eight patterns. If the booth sets 000 aside to mean “no ticket,” it still appears in the list. The booth has seven codes to hand out.

Encode a character as bytes

In plain words

Imagine a coat check.

Rule

Capital A has code point U+0041. Its UTF-8 byte is 41. Lowercase é has code point U+00E9. It uses two UTF-8 bytes, C3 first and A9 second.

A base letter followed by a separate combining accent can use two code points, one for the letter and one for the accent. Together they form a combining sequence. Use the single code point U+00E9 for each é in this lesson's inputs.

Step

  1. Use UTF-8. Start with an empty row and a byte count of zero. Write one box per byte and two hex digits per box.

  2. Read the input from left to right. For each letter, find its code point in The rule. Add that code point's bytes to the right of the row in the order shown.

  3. Stop once you have written the bytes for the last letter. Read the finished row left to right and count the boxes.

Trap

A character count alone leaves the UTF-8 byte count open. AA and Aé each have two letters here. AA takes two bytes, and Aé takes three. Work out how many bytes each code point adds before counting the total.

Decode a tiny bitmap

In plain words

Imagine leaving a note for a friend about two squares side by side on a calendar.

Rule

The paper format is the set of rules that tells you how to read the bits. It gives you the width, height, colour key and reading order. Both dimensions are whole numbers of at least one. Multiply the width by the height to count the squares. You need exactly that many bits, including every 0.

For the two grids shown here, the key is 1 for ink and 0 for white paper. Start with an all-white grid and the first bit. Work across each row from left to right, starting with the top row. Stop when you use the last bit in the bottom-right square.

Step

  1. Draw the given number of columns and rows. Leave all squares white. Count the squares and the bits. If the counts differ, stop before shading and report the two counts.

  2. Start at the top-left square with the first bit. Use the key to decide whether to shade the square or leave it white.

  3. Use the next bit in the next square to the right. At the end of a row, move to the leftmost square of the next row down if there is one. Stop after you use the last bit in the bottom-right square.

  4. Read the drawing back from the top left in the same order. Use the key to write one bit per square and join the rows into one line with no spaces.

Trap

Use the width to decide where to end each row. Grid A needs three bits across each of its two rows. If you use the height of two as the row length and lay out all six bits, the third bit starts a new row and you get Grid B. Check the key as well. If you swap what 0 and 1 mean, every ink square becomes white and every white square becomes ink.