🔢 Number Systems¶
Understanding how computers count differently than humans
What You'll Learn¶
- Why computers use binary (base-2)
- How to convert between number systems
- Decimal, binary, octal, and hexadecimal
- Python's built-in conversion functions
The Story of Counting¶
Humans have ten fingers, so we naturally count in base-10 (decimal). Computers have electrical circuits that are either ON or OFF, so they count in base-2 (binary).
Think of it like different languages: - Decimal: English (what humans speak) - Binary: Computer's native language - Hexadecimal: A compact way to write binary (like shorthand)
Decimal (Base-10) 🧮¶
The number system you're most familiar with. Uses digits 0-9.
How it works:
Each position represents a power of 10.
Binary (Base-2) 💻¶
Computers only understand 0s and 1s (OFF and ON).
How it works:
Each position represents a power of 2.
Why Binary?¶
- Electrical circuits have two states: ON (1) or OFF (0)
- Magnetic storage: North/South polarity
- CDs/DVDs: Pits and lands
- Simple and reliable!
Octal (Base-8) 🐙¶
Uses digits 0-7. Each octal digit represents 3 binary digits.
Conversion:
Less common today but still used in:
- Unix file permissions (e.g., chmod 755)
- Legacy systems
Hexadecimal (Base-16) 🔮¶
Uses digits 0-9 and letters A-F. Each hex digit represents 4 binary digits.
Hex: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F, 10...
A = 10 B = 11 C = 12 D = 13 E = 14 F = 15
Conversion:
Why Hexadecimal?¶
- More compact than binary
- Easy conversion (4 bits = 1 hex digit)
- Used everywhere in computing:
- Color codes:
#FF5733 - Memory addresses:
0x7FFF5E - MAC addresses
- Hash values
Python Conversion Functions¶
Python makes conversions easy with built-in functions:
# To binary
bin(10) # Returns '0b1010'
# To octal
oct(10) # Returns '0o12'
# To hexadecimal
hex(10) # Returns '0xa'
# To decimal (from any base)
int('1010', 2) # Returns 10
int('12', 8) # Returns 10
int('a', 16) # Returns 10
The prefixes:
- 0b = binary
- 0o = octal
- 0x = hexadecimal
Conversion Methods¶
Binary to Decimal¶
Multiply each bit by its place value and sum:
Decimal to Binary¶
Repeatedly divide by 2, collect remainders (read bottom-up):
13 ÷ 2 = 6 remainder 1
6 ÷ 2 = 3 remainder 0
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
Result: 1101 (read remainders bottom-up)
Quick Reference Table¶
| Decimal | Binary | Octal | Hexadecimal |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 |
| 2 | 10 | 2 | 2 |
| 3 | 11 | 3 | 3 |
| 4 | 100 | 4 | 4 |
| 5 | 101 | 5 | 5 |
| 6 | 110 | 6 | 6 |
| 7 | 111 | 7 | 7 |
| 8 | 1000 | 10 | 8 |
| 9 | 1001 | 11 | 9 |
| 10 | 1010 | 12 | A |
| 11 | 1011 | 13 | B |
| 12 | 1100 | 14 | C |
| 13 | 1101 | 15 | D |
| 14 | 1110 | 16 | E |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
Common Mistakes ⚠️¶
1. Forgetting the prefix¶
# Wrong
int('1010') # This is decimal 1010, not binary!
# Right
int('1010', 2) # Specify base=2 for binary
2. Reading binary remainders in wrong order¶
Decimal 5 to binary:
5 ÷ 2 = 2 R 1
2 ÷ 2 = 1 R 0
1 ÷ 2 = 0 R 1
Result: 101 (NOT 011!)
Read from bottom to top!
3. Confusing letter values in hex¶
4. Off-by-one in binary place values¶
Try It Out! 🚀¶
Run examples.py to see number systems in action:
Then try exercises.py to practice:
Key Takeaways¶
- Binary (base-2) is the language of computers (0s and 1s)
- Hexadecimal (base-16) is a compact way to write binary
- Octal (base-8) is still used for file permissions
- Python provides
bin(),oct(),hex(), andint()for conversions - Understanding number systems helps you debug and work with low-level code
Next: Bitwise Operations →