TwiddleLab / Code / How Computers Store Everything
Switches Count
What you will learn
- A bit is one switch: 0 (off) or 1 (on).
- Each new switch doubles the patterns: 3 switches make 8, 4 make 16.
- In a binary number the weights double from the right: 1, 2, 4, 8, 16. Add the lit ones.
- +1 works like an odometer: lit switches on the right go out and the carry lights the next one.
How the lesson goes
Your phone is full of tiny switches. How can switches count?
Start small: three switches. Flip them and collect every different pattern.
Here is the counting order computers use.
Press +1 again and again, or Run. Watch what happens on the way from 7 to 8.
Each switch is worth a number. Make 13.
Watch the weights appear above the switches. Lit weights add up.
Bits and binary.
A bit is one switch: 0 for off, 1 for on. Every number, letter and photo in a computer is stored as bits.
A binary number writes a number with bits. The weights double from the right: 1, 2, 4, 8, 16. Add the weights of the 1s: 1101 is 8 + 4 + 1 = 13.
n switches make 2 x 2 x ... x 2 = 2ⁿ patterns. Four bits count from 0 to 15.
Set the slider to the day of the month you were born.
Five switches follow the slider. That is your day in binary.
Where does 1011 sit on the number line?
Drag the marker to the number these switches make.
Number rush: hit every target in as few flips as you can.
Six targets. The last one is yours.
Next lesson: Bytes and Overflow · Part of the course: How Computers Store Everything
Lessons are interactive and need JavaScript. Turn it on to play them.