Back

Activity 2.14: Binary Numbers and What Bits Represent

divider

Activity 2.14

Binary Numbers and What Bits Represent

Key Concepts

Place Value in Base 2

Converting Both Ways

Comparing Binary Numbers

The Same Bits, Different Meanings

Activity 2.12's Doubling(64) Printed 0

A long holds 64 bits, and two to the 64th power needed one more. The answer ran out of room and came out as zero.

To see why, you need to see what a number is made of. In Activity 1.2 you flipped eight switches and counted 256 combinations. Today you calculate with them: turn a number into bits, bits back into a number, and see that the bits themselves do not say what they mean.

Base 10 Already Works This Way

You read 4271 without thinking about it: four thousands, two hundreds, seven tens, one one. Each position is worth ten times the position to its right.

Value Trace
4271 = 4 thousands + 2 hundreds + 7 tens + 1 one
place value 1000 100 10 1
position 3 2 1 0

Base 10 Already Works This Way

The College Board's wording: a digit's place value is the base raised to the power of its position, with positions counted from 0 on the right. In base 10, position 3 is worth 10 × 10 × 10.

Base 2 changes the ten to a two. Nothing else changes. There are only two digits, 0 and 1, and each place is worth twice the one to its right.

Eight Places Make a Byte

A bit is one binary digit. Eight of them make a byte. Predict the place values of all eight positions before they appear.

Value Trace
place value 128 64 32 16 8 4 2 1
position 7 6 5 4 3 2 1 0

Eight Places Make a Byte

All eight on is 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255. With zero as well, that is 256 different values — Activity 1.2's count, now explained.

A ninth place would be worth 256. A byte has nowhere to put it, which is the start of why Doubling ran out of room.

Binary to Decimal: Add the Places That Are On

Each bit is either worth its place value or worth nothing. Work out 10110110 before the answer appears.

Value Trace
bits 1 0 1 1 0 1 1 0
place value 128 64 32 16 8 4 2 1
counts 128 . 32 16 . 4 2 .
128 + 32 + 16 + 4 + 2 = 182

Decimal to Binary: Keep Halving

Going the other way needs Activity 1.6's two tools. % 2 gives the remainder when you halve, which is the rightmost bit. / 2 halves what is left, and C#'s integer division throws the remainder away for you.

Repeat until nothing is left.

Decimal to Binary: Keep Halving

Value Trace
182 % 2 = 0 bit 0 182 / 2 = 91
91 % 2 = 1 bit 1 91 / 2 = 45
45 % 2 = 1 bit 2 45 / 2 = 22
22 % 2 = 0 bit 3 22 / 2 = 11
11 % 2 = 1 bit 4 11 / 2 = 5
5 % 2 = 1 bit 5 5 / 2 = 2
2 % 2 = 0 bit 6 2 / 2 = 1
1 % 2 = 1 bit 7 1 / 2 = 0 stop
read from bit 7 down to bit 0: 1 0 1 1 0 1 1 0

The bits come out right to left: the first remainder is bit 0. So a program has to fill its bits from the end backward, or print them in reverse.

Comparing Binary Numbers

Which is bigger: 111 or 1010? Decide before the next line.

7 and 10. With no leading zeros, the longer one is bigger, because its highest place is worth more than all the smaller places put together.

Same length? Compare from the left. The first place where they differ decides it, exactly as with 4271 and 4199.

One Byte, Four Readings

Here is a single byte. What does it mean?

Value Trace
01000001
as a number 65
as a character A
as a switch bank off on off off off off off on
as part of a color a very dark red

One Byte, Four Readings

C# can be told to read 65 as a character. (char) asks for that reading; you will not need it again in this course. Predict what it prints.

Demo
Console.WriteLine((char)65);
Demo
A

Bits Have No Meaning of Their Own

The College Board says it directly: “The same sequence of bits may represent different types of data in different contexts.”

Nothing in 01000001 says number or letter. The meaning comes from the program reading it. That is why opening a photo in a text editor shows garbage: the right bits, read the wrong way.

Grouping Bits Is Abstraction

“Bits are grouped to represent abstractions,” including numbers, characters and color. Eight bits become a number; a number becomes a letter; three numbers become a color.

Each layer lets you stop thinking about the one below it — Activity 2.5's abstraction, one level further down. You chose a Raylib color without ever thinking about its 24 bits.

An Aside, Not on the Exam: Hex

Color codes like #3B82F6 are three bytes written in base 16, using 0–9 and then A–F for ten to fifteen. Four bits make one hex digit, so a byte is exactly two.

It is not in the course framework, so it will not be on the exam. It is worth five minutes because you will meet it every time you pick a color on a website.

Today's Objectives

  • Calculating the binary form of a decimal number, and the reverse
  • Explaining place value as the base raised to the power of the position
  • Comparing and ordering binary numbers
  • Explaining why the same bits can represent different types of data

Key Terms

Bit
A binary digit: 0 or 1.
Byte
Eight bits.
Place value
The base raised to the power of a digit's position, counting from 0 on the right.
Abstraction
Reducing complexity by focusing on the main idea.

'F' → Fullscreen

divider

Same Idea, Exam Notation

The deck's halving, as a C# loop that fills an array of eight bits from the right, and the exam's version of it.

C#
for (int position = 7; position >= 0; position--)
{
lamps[position] = left % 2;
left = left / 2;
}
Exam Notation
REPEAT UNTIL(left = 0)
{
bit ← left MOD 2
INSERT(bits, 1, bit)
left ← (left - bit) / 2
}

1. The Exam Builds a List of Bits

The C# loop fills a fixed array of eight from the right. The exam version starts with an empty list, bits ← [], and puts each new bit at the front with INSERT(bits, 1, bit). APPEND would put it at the end, and the number would come out backward.

2. Subtract the Remainder, Then Divide — Again

C#'s left / 2 throws the remainder away by itself. The reference sheet's / does not, so the exam writes (left - bit) / 2: the same MOD trick as the binary-search midpoint in Activity 2.10. The reference sheet is small, so the same few operators keep coming back.

divider

Build: Raylib Lab 10

Today's build is Raylib Lab 10, straight after this deck. A color is three bytes, and you draw a palette from those numbers, then take bits away and watch what happens: the deck's place values, turned into something you can see.

Go to Activity 2.15: Raylib Lab 10 — Color as Data

Raylib programs run on the classroom computers, so this one is built in class. Everything you hand in today is listed on the lab's page.

divider

On to the Lab