Binary & Hex
Every value in your game — health, coins, a sprite's color, the position of a turret — is a stack of bits in memory. This is the reference for reading them: base 2, base 16, the ranges the exam asks about, and the overflow you can trigger in your own game with one line of code.
Why base 2 exists
Hardware holds only two reliable voltage states — high and low, on
and off, 1 and 0. Everything the machine does must be expressible
as answers to yes/no questions, so every number, every letter,
every frame of your game is bits. You write 5; the
machine reads 101.
Reading binary
Each place is worth twice the place to its right — powers of two. To read a binary number, add the values where a 1 sits:
| Place | 2³ = 8 | 2² = 4 | 2¹ = 2 | 2⁰ = 1 | Total |
|---|---|---|---|---|---|
| 1011 | 1 → 8 | 0 | 1 → 2 | 1 → 1 | 11 |
| 0110 | 0 | 1 → 4 | 1 → 2 | 0 | 6 |
| 1111 | 1 → 8 | 1 → 4 | 1 → 2 | 1 → 1 | 15 |
Two facts the exam builds questions on: all ones is always one less than the next power of two (1111 = 16 − 1), and n bits count 0 through 2ⁿ − 1. Four bits: 0–15. Eight bits (a byte): 0–255. Thirty-two bits (a C# int): up to 2,147,483,647 — a number you have seen in 9.1's overflow demo.
Converting, by hand
- Decimal → binary: subtract the largest power of two that fits, repeat. 13 = 8 + 4 + 1 = 1101.
- Binary → decimal: add the place values where bits are 1 (the table above).
- Count up: the rightmost bit flips every step; carry left when a place rolls past 1, exactly like 9 → 10 in decimal.
Hexadecimal — binary with better handwriting
One hex digit covers exactly four bits, so a byte is always two hex digits and 8 bits never needs 8 written characters. 0xFF = 255. 0x10 = 16 (yes, sixteen — it is one sixteen and zero ones, not ten).
| Dec | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Hex | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
| Bin | 0000 | 0001 | 0010 | 0011 | 0100 | 0101 | 0110 | 0111 | 1000 | 1001 | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 |
Where you touch hex this year: sprite colors — Unity's Color32 is four bytes (R, G, B, alpha), and the site's theme blue #6cb2ff is (108, 178, 255) in decimal. Where the web touches hex: every CSS color. The details are in 9.2.
Overflow — the bug you can build in one line
Debug.Log(int.MaxValue + 1); // prints -2147483648
Add 1 to the largest 32-bit signed int and the sum cannot fit, so the bit pattern wraps around to the most negative number — the sign bit itself. The exam loves this as "rounding" for integers; the same idea in binary arithmetic is called overflow. Your game will not hit it with honest play (2.1 billion coins!), but any code that adds without checking can, and a cheat engine can make your coins negative in a heartbeat. Guard clauses are not just about nulls — they are about ranges.
Floats are bits too — and they lie
Debug.Log(0.1f + 0.2f == 0.3f); // False!
A float stores numbers in binary scientific notation (sign, exponent, fraction bits), and 0.1 has no exact binary representation — the same way 1/3 has no exact decimal one. The comparison is close but not equal. That is why game code compares floats with tolerances, or uses ints for anything that must count exactly (coins are int for this reason — 5.1's right-size-the-data argument, now with bit-level proof).
The exam-question shapes
- "How many distinct values can be represented with n bits?" — 2ⁿ. Name the RANGE too if asked: 0 to 2ⁿ−1 (unsigned) or −2ⁿ⁻¹ to 2ⁿ⁻¹−1 (signed)
- "A game stores health as an 8-bit binary number; max health?" — 255. Reason with the power, not a memorized number
- "Adding one bit doubles the range" — why: the new leftmost place doubles every existing pattern's headroom
- "Which is the largest?" — four binary/hex/decimal values, normalized to one base first
Where you meet bits this year
- 9.1 — Binary — the lesson with the finger-counting warmup
- 9.2 — Hex, RGB — colors as bytes, palettes as data
- 9.4 — Your Scene Is Data — the files all this lives inside