Skip to content
Qyubie Learn

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:

Placehundreds (10²)tens (10¹)ones (10⁰)
Digit507
Value50007

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 value1286432168421
Power of 22⁷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 value1286432168421
Bit10110110

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

What is the binary number 1101 in decimal?

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:

DivideResultRemainder
13 ÷ 261
6 ÷ 230
3 ÷ 211
1 ÷ 201

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.

BitsPatternsRange
120–1
4160–15
8 (one byte)2560–255
1665,5360–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

How many different values can you store in 5 bits?

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