Digital Logic
Binary numbers explained
Binary is counting with only two digits, 0 and 1. Each digit is a bit, and each bit's place is worth double the one to its right: 1, 2, 4, 8, then 16, 32 and so on. To read a binary number, add up the places that hold a 1. So 0101 is 4 + 1 = 5, and 1111 is 8 + 4 + 2 + 1 = 15.
One wire, one bit
In an ordinary circuit, a wire can sit at any voltage. In digital logic a wire carries just one of two things: 1 or 0, high or low. On a 5 V board, a 1 is anything at or above 3.0 V and a 0 anything at or below 1.5 V. The wide gap between those two thresholds is what makes digital signals hard to corrupt: noise has to push a long way before a bit flips.
One wire can say two things. Put four side by side, give each a place, and together they say sixteen: every number from 0 to 15.
Live experiment
Four wires make a number
Four switches, worth 8, 4, 2 and 1, each light a probe, and a display shows what the switched-on places add up to. Make 5, then 10, then 15, the most four wires can say.
“One or zero” is lesson 1 of 6 in Gate Grove, Digital Logic World 1. Free with an account. It’s open as soon as you sign up.
Why the places double
Places worth 1, 2, 4 and 8 make every number from 0 to 15, each in exactly one way. That works because each place is one more than all the places below it put together. Places worth 1, 2, 3 and 4 would waste combinations (3 is both 3 and 1 + 2) and only reach 10.
In general, n bits make 2ⁿ patterns, counting from 0 to 2ⁿ − 1. Eight bits, a byte, count to 255. Ten bits count to 1023, which is why an Arduino's 10-bit analog input reads from 0 to 1023.
Reading and writing it
Binary to decimal: multiply each bit by its place value and add. For 1011:
Decimal to binary: start at the biggest place that fits, write a 1 and subtract, then work down. For 11: 8 fits (3 left), 4 doesn't, 2 fits (1 left), 1 fits. So 11 is 1011.
Where you'll meet it
The word "bit" is short for binary digit. Programmers often write binary in hexadecimal, one digit per group of four bits, 0–9 then A–F for 10–15, so 1101 is D and a full byte 1111 1111 is FF, which is 255. Every photo, song and message on your phone is stored as bits, and a number display's decoder turns its four input bits into the digit you see.
Free lesson
Now build it yourself
Each lesson comes in three short parts: see the idea, try it on a live board, then build it yourself, with quick checks as you go.
“One or zero” is lesson 1 of 6 in Gate Grove, Digital Logic World 1. Free with an account. It’s open as soon as you sign up.
Or start from the very first lesson, no account neededWork it out
- Binary, decimal and hex converterDecimal, binary and hex, with every bit's place value added up.
- Binary and hex practice problemsBinary to decimal and back, bits needed, the largest n-bit number and hex.
- Binary, decimal and hex table0 to 15 in all three bases, the powers of two, common bytes, and converting four bits at a time.
Practice questions
Three quick questions from the lessons. Pick an answer to check it.
Show the answer
Answer: 8s, 2s and 1s. 8 + 2 + 1 = 11. Start from the biggest place that fits and work down.
- Why not “4s, 2s and 1s”? 4 + 2 + 1 = 7, four short. 11 is bigger than 8, so the 8s place has to be on.
- Why not “8s and 2s”? 8 + 2 = 10, one short. The 1s place has to be on too, to make 11.
- Why not “8s and 4s”? 8 + 4 = 12, one too many. Once the 8 is on, only 3 is left, and 4 doesn't fit in 3.
Show the answer
Answer: Twice the place to its right. 1, 2, 4, 8, 16…: doubling, where everyday numbers go up by ten.
- Why not “Ten times the place to its right”? Ten times is how everyday decimal places grow: 1, 10, 100. Binary has only two digits, so each place doubles: 1, 2, 4, 8.
- Why not “One more than the place to its right”? Then the places would go 1, 2, 3, 4. In binary they double: 1, 2, 4, 8, 16.
- Why not “The same”? If every place were worth the same, it wouldn't matter where a 1 sat. In binary each place is worth twice the one to its right.
Show the answer
Answer: 1 or 0, nothing in between. That's the whole deal with digital. A wire is either on (1) or off (0). Anything more complicated is built from lots of those.
- Why not “Any voltage from 0 to 5”? A voltage that can take any value is an analog signal. A logic circuit reads each wire as one of two levels, 0 or 1.
- Why not “A number from 0 to 15”? One wire carries one bit. A number from 0 to 15 takes four wires, one for each place: 8, 4, 2 and 1.
- Why not “Only 1”? A wire stuck at 1 couldn't carry any information. It has to be able to switch between 1 and 0.
“One or zero” is lesson 1 of 6 in Gate Grove, Digital Logic World 1. Free with an account. It’s open as soon as you sign up.
Questions people ask
How do you convert binary to decimal?
Multiply each bit by its place value and add them up. The places, from the right, are worth 1, 2, 4, 8, 16 and so on, each double the last. So 1011 is 8 + 0 + 2 + 1 = 11.
How do you convert decimal to binary?
Start at the biggest place value that fits, write a 1 there and subtract it, then move down a place: write 1 if the place fits what's left and 0 if it doesn't. For 11: 8 fits (3 left), 4 doesn't, 2 fits (1 left), 1 fits, so 11 is 1011.
How high can n bits count?
n bits make 2ⁿ different patterns, so they count from 0 to 2ⁿ − 1. Four bits reach 15, eight bits (a byte) reach 255, and ten bits, like an Arduino's analog input, reach 1023.
Why do computers use binary?
Because a wire that only has to be high or low is very hard to misread. Noise has to push a signal a long way, past a wide gap between the two levels, before a 1 can be mistaken for a 0. Ten levels squeezed onto the same wire would sit so close that a little noise could change a digit.