Computer Science FundamentalsLesson 1 of 3 · tap to see all lessons
Computer Science · Beginner
Binary Numbers Explained: How Computers Count
Understand how binary works, convert between binary and decimal by hand, and check your answers with short Python programs you can run here.
- Qyubie Learn Editorial Team
- Published
- 7 min read
Includes 2 exercises and 2 quick checks you can do right here.
Inside a computer, everything — numbers, letters, photos, this page — is stored as long rows of 0s and 1s. That two-symbol system is called binary. Once you see how binary counting works, a lot of computing stops being mysterious.
By the end of this guide you will be able to:
- explain why computers use binary,
- read a binary number by its place values,
- convert binary to decimal and decimal to binary by hand, and
- check your conversions with Python.
What you need: basic arithmetic (adding and multiplying by 2). The Python examples are optional helpers — press Run Python to try them.
Why two symbols?
A computer's memory is made of billions of tiny electronic switches. Each switch is either off or on, and we write those two states as 0 and 1. One 0-or-1 value is called a bit (short for binary digit). Eight bits make a byte.
Two states are easy to build reliably and hard to confuse, even with electrical noise. So instead of switches with ten levels for our ten digits, computers use many simple two-level switches and count in base 2.
Place values: decimal first
You already know a place-value system. In the decimal number 507, each position is worth ten times the one to its right:
| Place | hundreds (10²) | tens (10¹) | ones (10⁰) |
|---|---|---|---|
| Digit | 5 | 0 | 7 |
| Value | 500 | 0 | 7 |
500 + 0 + 7 = 507. Decimal is base 10: ten digits (0–9) and places that grow by ×10.
Place values in binary
Binary works the same way, but it's base 2: only the digits 0 and 1, and each place is worth twice the one to its right.
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Power of 2 | 2⁷ | 2⁶ | 2⁵ | 2⁴ | 2³ | 2² | 2¹ | 2⁰ |
A 1 in a place means "count this value"; a 0 means "skip it".
Binary to decimal
To convert, write the place values above the bits and add the values that have a 1.
Take 1011 0110 (the space just makes it easier to read):
| Place value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Bit | 1 | 0 | 1 | 1 | 0 | 1 | 1 | 0 |
128 + 32 + 16 + 4 + 2 = 182.
Python can check this for you. int(text, 2) reads text as a base-2 number:
Try it · Python
Quick check
Decimal to binary: repeated division by 2
To go the other way, divide by 2 again and again, writing down each remainder, until you reach 0. Then read the remainders from bottom to top.
Convert 13:
| Divide | Result | Remainder |
|---|---|---|
| 13 ÷ 2 | 6 | 1 |
| 6 ÷ 2 | 3 | 0 |
| 3 ÷ 2 | 1 | 1 |
| 1 ÷ 2 | 0 | 1 |
Reading the remainders upward gives 1101 — and we just checked above that 1101 is 13.
Here is the same method as a Python program. // is whole-number division and % gives the remainder:
Try it · Python
Python also has a built-in shortcut, bin(). Its answer starts with 0b, which just marks the number as binary:
Try it · Python
Exercise · Python
Write your own converter
Using the repeated-division method (not bin() or format()), convert the number 37 to binary and print just the bits.
Expected output
100101
Hint
Inside a while n > 0 loop: put str(n % 2) in front of bits, then set n = n // 2.
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
n = 37
bits = ""
while n > 0:
bits = str(n % 2) + bits
n = n // 2
print(bits)
Counting in binary
Counting up in binary follows one rule: when a place would go past 1, it goes back to 0 and you carry 1 to the left — just like 9 → 10 in decimal.
Try it · Python
Notice the pattern: the rightmost bit flips every step, the next one every 2 steps, the next every 4, and so on.
How many values fit in n bits?
Each extra bit doubles the number of patterns. With n bits you can write 2ⁿ different values, from 0 up to 2ⁿ − 1.
| Bits | Patterns | Range |
|---|---|---|
| 1 | 2 | 0–1 |
| 4 | 16 | 0–15 |
| 8 (one byte) | 256 | 0–255 |
| 16 | 65,536 | 0–65,535 |
That's why a byte holds 0 to 255, and why you see 255 so often in computing — for example in RGB colors, where each of red, green, and blue is one byte.
Quick check
Exercise · Python
Bytes in a table
Print the numbers 250 to 255, each as an 8-bit binary number, one per line in the form 250 = 11111010.
Expected output
250 = 11111010 251 = 11111011 252 = 11111100 253 = 11111101 254 = 11111110 255 = 11111111
Hint
for n in range(250, 256): then print(n, "=", format(n, "08b")).
Show a solution
Try it yourself first — you learn more from a wrong attempt than from reading an answer.
for n in range(250, 256):
print(n, "=", format(n, "08b"))
Common mistakes
- Reading remainders top to bottom. With repeated division, the first remainder is the rightmost bit. Read upward.
- Starting place values at 2. The rightmost place is worth 1 (2⁰), not 2.
- Confusing "largest value" with "number of values". 8 bits give 256 values, but the largest is 255.
Summary
- Binary is base 2: digits 0 and 1, place values that double (1, 2, 4, 8, …).
- Binary → decimal: add the place values where there's a 1.
- Decimal → binary: divide by 2 repeatedly and read the remainders from bottom to top.
- n bits hold 2ⁿ values, from 0 to 2ⁿ − 1.
Next steps: convert your age and your birth year to binary by hand, then check them with bin() in the playground.
References and further reading
Keep learning
- ProgrammingBeginner
Loops in Python: for, while, and When to Use Each
Learn how Python's for and while loops work, how range() counts, and how to avoid endless loops — with exercises you can run in your browser.
11 min read5 practice itemsUpdated December 5, 2026 - ProgrammingBeginner
Java Arrays and Methods: 1D and 2D Arrays, Searching, Parameters, and Return Values
Store many values in Java arrays, loop through 1D and 2D arrays, search them, and organize code into methods with parameters, return values, and overloading.
10 min read2 practice itemsUpdated December 5, 2026 - ProgrammingBeginner
Java Program Structure and Syntax: Your First Program Line by Line
Understand every line of a first Java program: classes, main, statements, comments, identifiers, keywords, and the three kinds of errors.
8 min read4 practice itemsUpdated December 5, 2026