← Back to course

Binary & Number Representation

You’ll be able to

Everything is bits

At the lowest level a computer stores and moves information as bits — binary digits, each either 0 or 1, physically an "off" or "on" state. Numbers, text, images, and sound are all represented as sequences of bits; the same 0s and 1s mean different things depending on how the program interprets them. A group of 8 bits is a byte. Binary works for the same reason our familiar decimal (base-10) system works, except each place is worth a power of 2 instead of a power of 10.

Place value in base 2

In binary, reading right to left, the place values are 1, 2, 4, 8, 16, 32, 64, 128, … — each place worth twice the one before it (2⁰, 2¹, 2², …). To find a binary number’s decimal value, add up the place values wherever there is a 1. For example, 1011 has 1s in the 8, 2, and 1 places: 8 + 2 + 1 = 11. Every whole number has exactly one binary representation, just as it has one decimal representation.

Bits and range: powers of two

The number of bits available limits how many different values you can represent. With n bits you can make 2ⁿ distinct patterns, representing the unsigned integers 0 through 2ⁿ − 1. Four bits give 2⁴ = 16 patterns (0–15); eight bits give 2⁸ = 256 patterns (0–255). This is why a value can overflow: if a number grows past the largest pattern the bits can hold, it cannot be represented and the result is wrong.

Range of n bits
patterns = 2ⁿ · largest unsigned value = 2ⁿ − 1
n bits produce 2ⁿ distinct patterns, representing the integers 0 through 2ⁿ − 1. Each added bit doubles the number of representable values.
Worked example

Convert the decimal number 45 to 8-bit binary.

  1. 1.Write the 8-bit place values: 128, 64, 32, 16, 8, 4, 2, 1.
  2. 2.Find the largest place value that fits in 45: 32 fits (45 − 32 = 13). Place a 1 in the 32s place.
  3. 3.From the remaining 13: 16 is too big (0), 8 fits (13 − 8 = 5), 4 fits (5 − 4 = 1), 2 is too big (0), 1 fits (1 − 1 = 0).
  4. 4.Place 1s in the 32, 8, 4, and 1 places and 0s elsewhere: 0 0 1 0 1 1 0 1.
  5. 5.Check: 32 + 8 + 4 + 1 = 45. ✓
Answer: 45 in 8-bit binary is 00101101 (32 + 8 + 4 + 1 = 45).
Checkpoint

What is the decimal value of the binary number `1101`?

Tip

Memorize the low powers of two: 1, 2, 4, 8, 16, 32, 64, 128, 256. Almost every binary conversion and range question on the exam comes straight from this list.

Checkpoint

A game stores a player’s score in a single unsigned byte (8 bits). What is the largest score it can represent, and what happens if the score should reach 256?

On the exam

Watch for overflow: with a fixed number of bits, the representable range is 0 to 2ⁿ − 1. A value past the top cannot be stored, so the exam expects you to say it overflows — not that it rounds or is stored fine.

Answer the 2 checkpoints as you read.

Sign in to save your progress