1 / 6 · Intro
Bits and binary numbers
Eight switches can show 256 different patterns, enough for every number from 0 to 255.
Jump to the 10 practice questionsYou will learn
- Read a row of bits as a number
- Write a number in binary
1Explore
Switches that make a number
Before reading anything, try the switches below. Press All off. Now turn on only the rightmost switch and read the line under the row: 1. Turn it off and switch on the one to its left: 2. The next one gives 4, and the one after that 8. Keep moving left. By the far end you have met every value a switch in a byte can have, and the last one is worth 128.
Drag to turn. Scroll or pinch to zoom.
2Problem
Read 1011
What is 1011 in decimal?
Hint
The 8, 2 and 1 switches are on.
Solution
Answer: 11
8 + 2 + 1 = 11.
3Explain
Each switch is worth twice the one to its right
What you just saw is the whole idea of binary. In decimal each place is worth ten times the place to its right: ones, tens, hundreds. In binary each place is worth twice as much, so the values run 1, 2, 4, 8, 16, 32, 64, 128. To read a pattern, add the values of the switches that are on. 00100101 has the 32, 4 and 1 switches on, which makes 32 + 4 + 1 = 37. Switch on exactly those three and the sum line should agree. One more rule drops out for free.
The eight places of a byte. The shaded switches are on, and their values add up to the number.
4Problem
Read 1100100
What is 1100100 in decimal?
Hint
Seven places: 64, 32, 16, 8, 4, 2, 1.
Solution
Answer: 100
64 + 32 + 4 = 100.
5Problem
Odd or even
Which of these numbers is odd?
Hint
Only the rightmost place is worth an odd amount.
Solution
Answer: 10011
A binary number is odd when its last bit, the 1s place, is 1.
6Explain
Why a chip uses two states instead of ten
Why not ten states, one for each decimal digit? The voltage on a wire inside a chip is never exact. Heat, neighbouring wires and small differences between transistors push it up and down a little all the time. A circuit that only has to tell high from low can shrug most of that off, because anything above a middle line counts as 1 and anything below counts as 0. Squeeze ten levels into the same range and they sit so close together that a little noise could make one read as its neighbour.
7Explore
Writing 100 with the switches
Going the other way, from a number to bits, works like paying with the biggest coin that fits. Take 100. 128 is too big. 64 fits, leaving 36. 32 fits, leaving 4. 16 and 8 are both too big for 4. 4 fits, leaving 0. So the 64, 32 and 4 switches are on: 1100100, or 01100100 as a full byte. Pick 100 as the target in the scene and check. Some people prefer a second method. Divide by 2 again and again, write down each remainder, then read the remainders from the bottom up.
toString(2) writes a number in binary; padStart fills it out to a full byte of eight bits.
const n = 100;
console.log(n.toString(2));
console.log(n.toString(2).padStart(8, "0"));1100100
011001008Problem
Write 37
Hint
37 = 32 + 4 + 1.
Solution
Answer: 100101
32 + 4 + 1: 1s in the 32, 4 and 1 places.
9Explain
One more switch doubles the patterns
How many different numbers can eight switches make? Every extra bit doubles the count. One bit gives 2 patterns. Two bits give 4: 00, 01, 10 and 11. Three give 8, and n bits give 2 to the power n. A byte, eight bits, gives 256 patterns, so a byte counts from 0 to 255, and that is also why each of the four numbers in an IPv4 address stops at 255. Need to give 1000 things their own number? 9 bits give only 512. You need 10, which give 1024.
10Problem
Four bits
How many patterns do 4 bits give?
Hint
2 × 2 × 2 × 2.
Solution
Answer: 16
2⁴ = 16, from 0000 to 1111.
11Problem
Eight bits
How many different patterns can 8 bits show?
Hint
Double eight times, starting from 1.
Solution
Answer: 256
2⁸ = 256.
12Problem
The largest byte
What is the largest number one byte can hold?
Hint
One of the patterns is zero.
Solution
Answer: 255
11111111 = 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255.
13Problem
Numbers for 1000 things
How many bits do you need to give 1000 things different numbers?
bits
Hint
9 bits give 512, which is not enough.
Solution
Answer: 10 bits
2⁹ = 512 is too few; 2¹⁰ = 1024 is enough.
14Explain
Counting up in binary
Press +1 a few times starting from 0 and watch the switches. It looks like a car's odometer with only two digits on each wheel. 0, then 1, then the 1s place rolls over and carries into the 2s place: 10. Then 11. Then both roll over at once: 100. With every switch on you have 11111111, which is 255. The real answer to 255 + 1 is 256, and it needs a ninth bit: 100000000. The scene keeps only eight bits, so +1 from 255 shows 0.
for (let n = 0; n <= 8; n++) {
console.log(n + " = " + n.toString(2));
}0 = 0
1 = 1
2 = 10
3 = 11
4 = 100
5 = 101
6 = 110
7 = 111
8 = 100015Problem
After 0111
Hint
All three 1s roll over and carry.
Solution
Answer: 1000
7 + 1 = 8: the three 1s roll over and carry into the 8s place.
16Problem
255 + 1
How many bits does 255 + 1 = 256 need?
bits
Hint
256 is 100000000.
Solution
Answer: 9 bits
256 = 100000000: a 1 followed by eight 0s.
17Practice
Binary by dividing by 2
Finish toBinary(n): divide n by 2 again and again and put each remainder in front of the digits you already have. Do not use toString.
100101
1100100
11111111function toBinary(n) {
let digits = "";
while (n > 0) {
// put the remainder in front, then halve n
}
return digits;
}
console.log(toBinary(37));
console.log(toBinary(100));
console.log(toBinary(255));
Hint
The first remainder is the rightmost digit, so each new one goes in front.
A solution
function toBinary(n) {
let digits = "";
while (n > 0) {
digits = (n % 2) + digits;
n = Math.floor(n / 2);
}
return digits;
}
console.log(toBinary(37));
console.log(toBinary(100));
console.log(toBinary(255));18Explain
Common mistakes
"10 means ten."
Only in decimal. In binary, 10 is one 2 and no 1s, so it means two. When a number could be read either way, write a small 2 after it, as in 10₂, to show which system you mean.
"More 1s means a bigger number."
Position matters more than the count. 01111111 has seven 1s and is 127. 10000000 has a single 1 and is 128. The leftmost switch alone outweighs all seven switches to its right, because 128 = 127 + 1.
"A byte can hold numbers up to 256."
A byte has 256 patterns, but one of them, 00000000, is used for zero. That leaves 255 as the largest. Four bits behave the same way: 16 patterns, largest 15.
19
Summary
- A bit is 0 or 1; a byte is 8 bits.
- Binary place values double: 1, 2, 4, 8, 16, 32, 64, 128.
- n bits give 2ⁿ patterns; a byte holds 0 to 255.
- Adding 1 to all 1s rolls over and needs one more bit.