Course outline · 0% complete

0/27 lessons0%

Course overview →

Binary: everything is bits

lesson 6-1 · ~11 min · 19/27

Quiz

Warm-up from lesson 5-3: what does this loop print? total = 0 for n in range(1, 4): total = total + n print(total)

Two symbols are enough

You can write software without knowing what is underneath, but engineers who do know debug faster and design better: binary explains why file sizes come in strange numbers, why colors max out at 255, and why computers sometimes get decimal math slightly wrong. This unit builds that mental model.

You have been typing text and numbers, but the machine underneath stores none of that directly. Deep down, a computer's memory is billions of tiny switches, each either off or on. We write those two states as 0 and 1, and one such switch's value is called a bit (binary digit).

How can 0s and 1s represent the number 214, or the letter A, or a photo? The same way ordinary decimal numbers work. In decimal, each position is worth 10 times the one to its right (ones, tens, hundreds). Binary does the identical thing with 2 instead of 10: positions are worth 1, 2, 4, 8, 16, 32, 64, 128...

So binary 1011 means, reading right to left: 1×1 + 1×2 + 0×4 + 1×8 = 11.

A group of 8 bits is a byte, enough for numbers 0 to 255. Letters are just numbers by agreement (the letter A is 65), colors are three numbers (red, green, blue), and images are grids of colors. Everything is bits.

one byte: 1101 0110 = 214110101101286432168421128 + 64 + 16 + 4 + 2 = 214add the place values under the 1 bits
Each bit position has a place value (1, 2, 4, 8, ... doubling leftward). A byte's number is the sum of the place values where a 1 sits.

Code exercise · python

Python can read binary directly using the 0b prefix. Run it, then verify each by adding place values yourself.

Problem

Convert binary 1010 to a decimal number. (Place values right to left: 1, 2, 4, 8.)

Code exercise · python

Your turn. A byte with all eight bits on is 11111111. Print its decimal value using Python's 0b prefix (do not type the answer as a plain number).