← Back to course
2/6
Week 05 · Storing Data

Tuesday

Codes in binary
// From answers to codes to bits
⏱ about 20 min

Tuesday: Codes in Binary

Comet is building a tiny light panel for the Commons desk. It has two small lights, and each light is either on or off.

"On is 1 and off is 0," she explains. "Two lights can show every Q3 code!"

She tries code 1, then 2, then 3. Each one works.

Then she tries code 4, for bike rack. She stares at the two lights.

Wren leans in. "What do you notice?"

"I have run out of lights," Comet says slowly.

Nova hovers over the panel. "Would you like a hint?" she asks. "Count how many different patterns two lights can make."

Comet starts sketching on the back of the code book.

Place values in binary

Binary uses only the digits 0 and 1.

Each position has a place value. The place value is the base raised to the power of the position.

Positions are numbered from 0 at the right, going up by 1 for each step to the left.

In binary the base is 2. So the place values from the right are 1, 2 and 4.

Position210
Place value421

To read a binary number, add the place values of every position that holds a 1.

Q3 codeBinaryPlace values added
000nothing
1011
2102
3112 plus 1
41004
READ AND WRITE BINARY
  • Read the question.
  • Tap your answer.
What is the Q3 code 3 in binary?
What number does binary 10 stand for?
What number does binary 101 stand for?
What number does binary 110 stand for?

Two bits, four codes

Two bits can make only four patterns: 00, 01, 10 and 11. Those are the codes 0, 1, 2 and 3.

Code 4 needs a third bit: 100.

In many programming languages, whole numbers get a fixed number of bits. That limits the range of numbers.

Going past the range can cause an overflow error. Comet's two-light panel just hit that limit.

COUNT THE PATTERNS
  • Read the question.
  • Tap your answer.
How many different codes fit in 2 bits?
With 3 bits, the biggest code is 111. What number is that?
How could Comet fix her panel for code 4?
StatementTrue or false?
Binary uses only the digits 0 and 1.?
Positions are numbered starting from 0 at the right.?
Two bits can store the code 4.?
A fixed number of bits limits the range of whole numbers.?
WHY THIS EXERCISEEvery stored number lives in a fixed number of bits, so its limits matter.
When a number is too big for its fixed bits, it can cause an ____ error.
A group of 8 bits is called a ____.
READ A BINARY NUMBER
  • ?Find every position that holds a 1.
  • ?Add the place values of those positions.
  • ?Write the place values above the digits: 1 on the right, then 2, then 4.
  • ?Check your answer with the code book.
WHY THIS EXERCISEFollowing the same steps every time makes binary quick and reliable.
On paper, sketch Comet's fixed panel with three lights, set to show code 4 (on, off, off).

Nice work. Tomorrow in Count Lab you will shrink a column and see what can be rebuilt.

← Monday