◀ 8.69.19.2 ▶

9.1 — Binary: How Numbers Live in Memory

All semester you have typed int and float like they were words. Underneath, every one of your coins, every xSpeed of 5, every cooldown of 2f is a stack of bits — and once you can READ them, half the AP exam's data questions become easy points.

In this lesson you will be able to answer“how do I read a number as bits?”
Break It Down~10 min

Read the number 13 as eight bits of memory — and be able to say what every single bit is worth.

First, before anything else: cut that into parts small enough that you could look each one up on its own.

  1. Name the place valueswhat does this part do?Read: the place-value table in this lesson, in the Concept Lab
  2. Walk the places from largest to smallestwhat does this part do?Read: Step 2 of the Build Lab below
  3. Say why the hardware counts in twos and not tenswhat does this part do?Read: Binary & Hex, the unit reference page
  4. Work out how far a fixed width can reachwhat does this part do?Read: the byte-vs-int range discussion in the Concept Lab
  5. Recognise the edgewhat does this part do?Read: the overflow practice on the Exam Reference Sheet

The split is the whole lesson. "Convert 13 to binary" is one task you can only memorise; these five are five things you can look up, and the last two are the ones the exam actually asks about. Nothing here needs a variable — a bit width is a design decision you make once, and every later bug in this unit is that decision meeting a number that grew past it.

The test: could you explain this part to somebody using only a piece of paper? If a part needs Unity open to explain, it is not a part of this problem — it is a part of a coding problem.

Your turn

Pick two of the parts above and say in one line what each one actually does.

You want to store the number of lessons in the year, 81. Does it fit in 7 bits? Name the power of two that decides it, and say what that tells you about picking a width.

Unity spends a whole byte on a bool even though a bool is one bit. Is that waste a mistake? Argue one sentence for each side.

Concept Lab ~15 min

Base 10 first (you already know it). The number 273 means 2×100 + 7×10 + 3×1. Each place is worth TEN times the one to its right, because you have ten fingers' worth of symbols (0–9). Binary is the same game with two symbols:

Place8s4s2s1s
Bits1011
Worth8021

1011₂ = 8 + 0 + 2 + 1 = 11. Each place is a power of two: 1, 2, 4, 8, 16, 32, 64, 128.

Why computers chose 2. Every wire in the machine holds either HIGH or LOW, on or off, 1 or 0 — your game runs on billions of yes/no questions per second. An 8-bit number (a byte) counts 0–255; an int like your health (3) or coins (0) is 32 bits wide, so its range is enormous — but NOT infinite, and that is the fact that matters:

Your turret cooldown 2f stored as a float:
0 10000000 00000000000000000000000  → the bits of 2.0f
(int) 2f is 00000000000000000000000000000010  → the bits of 2

Socratic

Coins are an int, maxHealth is an int, jumpForce is a float. Why would coins NEVER want to be a float — and what goes wrong with money (0.1 + 0.2) in binary? Try it: Debug.Log(0.1f + 0.2f == 0.3f); — why does it print False?

Build Lab ~20 min

Step 1 — count in binary with your hands. Right thumb = 1, right index = 2, right middle = 4, right ring = 8, right pinky = 16. Count 0–31 by raising fingers. When you can get to your coin count without stalls, you have it.

Step 2 — read your own game's numbers. Convert, on paper, into 8-bit binary. Write the place values down, not just the bits:

  1. health = 3
  2. attackCooldown = 2
  3. maxAttackRange = 7
  4. patrolDistance = 3, chaseDistance = 5 → convert both
  5. The number of B days in the semester: 81 lessons → does it fit in 7 bits? (128 says yes, 64 says no — which one is right?)
Check your answers write yours first

Then compare. Every one of these is something you can now explain, and every one of them is a real value from your own scripts and your own calendar.

  • health = 3 → 00000011 (2 + 1)
  • attackCooldown = 2 → 00000010 (the 2s place alone)
  • maxAttackRange = 7 → 00000111 (4 + 2 + 1)
  • patrolDistance = 3 → 00000011; chaseDistance = 5 → 00000101 (4 + 1)
  • 81 fits: 7 bits is 128 values, 0–127, and 81 is inside that. The place that decides it is the 64s — 81 is bigger than 64, so that bit is a 1.

Step 3 — bit tricks your game ALREADY plays. direction = direction * -1 from 4.1? In binary, negation is a bit pattern operation (two's complement: flip every bit, add 1). And your booleans — hasAxe, isGrounded, isOnLadder — are single bits. A byte could hold EIGHT of your booleans. Unity spends a byte per bool anyway — memory is cheap now, but the exam wants you to KNOW a bool is a bit.

Checkpoint — before moving on, you must be able to:

Reference Docs

Need a deeper explanation? These pages cover the same concepts:

Next: 9.2 — Hex, RGB & Data Abstraction

0/4 answered

Answers save on this device as you type. At the end of class, click Download and upload the file to the Canvas assignment.

Saved on this device only — the Dashboard tracks your progress.