Computer Science Fundamentals from Scratch

Number Systems and Binary

Lesson 2 of 5 2 min read Updated 28 September 2026

Why binary?

Computers use electronic switches that are either on or off. That fits a system with only two digits: 0 and 1. This is the binary number system, and each digit is a bit. Eight bits make a byte.

Number systems

SystemBaseDigits
Decimal100-9
Binary20, 1
Octal80-7
Hexadecimal160-9, A-F

Binary to decimal

Each position is a power of 2, from the right: 1, 2, 4, 8, 16…

1011 = 1×8 + 0×4 + 1×2 + 1×1 = 11

Decimal to binary

Divide by 2 repeatedly and read the remainders from bottom to top.
13 ÷ 2 = 6 remainder 1
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
So 13 = 1101.

Hexadecimal

Hex is a compact way to write binary, since one hex digit equals four bits. It is used for colours (#2563EB) and memory addresses.

1111 1111 binary = FF hex = 255 decimal.

Try it in code

print(bin(13))         # 0b1101
print(int("1101", 2))  # 13
print(hex(255))        # 0xff
print(0b1010 + 0b0011) # 13

How text is stored

Every character has a number. ASCII maps English letters and symbols to numbers (A = 65). Unicode covers all languages and emoji. UTF-8 is the most common way to store Unicode.

Bytes and sizes

1 KB = 1024 bytes, 1 MB = 1024 KB, 1 GB = 1024 MB.

Negative numbers and decimals

Computers use two's complement for negative integers and floating point for decimals. Floating point can cause small errors: in many languages 0.1 + 0.2 does not equal exactly 0.3.

Practice

Convert 45 to binary, 10110 to decimal and 200 to hexadecimal. Check your answers with code.