Number Systems: Binary, Octal, Hex
Why do computers use binary?
Inside a computer, every circuit is like a tiny switch that is either ON or OFF — current flows or it does not. Two states are easy to make and very reliable, so computers represent everything with two digits: 1 (ON) and 0 (OFF). This is the binary number system. Numbers, letters, photos, music and even Python programs are finally stored as patterns of 0s and 1s.
Base (radix) and place value
The base (or radix) of a number system is the number of different digits it uses. In every positional system, the value of a digit depends on its position. Positions are numbered 0, 1, 2 … from the right, and each position has a weight of baseposition.
| System | Base | Digits used | Example |
|---|---|---|---|
| Decimal | 10 | 0 1 2 3 4 5 6 7 8 9 | (725)₁₀ |
| Binary | 2 | 0 1 | (1011)₂ |
| Octal | 8 | 0 1 2 3 4 5 6 7 | (375)₈ |
| Hexadecimal | 16 | 0–9 and A B C D E F (A=10 … F=15) | (2AF)₁₆ |
Decimal (base 10)
This is the system we use every day. In 725: 7 is in the hundreds place, 2 in the tens place and 5 in the ones place.
| Digit | Position | Weight | Value |
|---|---|---|---|
| 7 | 2 | 10² = 100 | 700 |
| 2 | 1 | 10¹ = 10 | 20 |
| 5 | 0 | 10⁰ = 1 | 5 |
Binary (base 2)
Only digits 0 and 1 are allowed. Each binary digit is a bit. Place values double from right to left: 1, 2, 4, 8, 16, 32, 64, 128 …
The right-most bit is the Least Significant Bit (LSB); the left-most is the Most Significant Bit (MSB). A binary number ending in 0 is even; ending in 1 is odd.
Octal (base 8)
Octal uses digits 0 to 7. Weights are 1, 8, 64, 512 … Since 8 = 2³, one octal digit = exactly 3 bits. Octal was popular in early computers and is still used for file permissions in Linux, e.g. chmod 755.
Example: (57)₈ = 5 × 8 + 7 × 1 = 40 + 7 = 47 in decimal.
Hexadecimal (base 16)
Hexadecimal needs 16 symbols, so after 9 we use letters: A = 10, B = 11, C = 12, D = 13, E = 14, F = 15. Since 16 = 2⁴, one hex digit = exactly 4 bits. Weights are 1, 16, 256, 4096 …
Example: (2F)₁₆ = 2 × 16 + 15 × 1 = 32 + 15 = 47.
Comparison table 0–16
Instead of copying a table, let Python print it for you. Look how hex reaches F at 15, and octal "carries" to 10 at 8.
print('Dec Bin Oct Hex')
for n in range(0, 17):
print(f'{n:>3} {n:>04b} {n:>3o} {n:>3X}')Learn binary of 0–15 by heart (the 4-bit patterns). With these 16 patterns you can convert any binary ↔ hex instantly, and the first 8 (3-bit) patterns give you binary ↔ octal.
Number systems in Python
Python has built-in functions to see a number in other bases: bin(), oct(), hex(). The prefixes 0b, 0o, 0x tell which base it is. int(text, base) converts back to decimal.
n = 45
print(bin(n)) # binary
print(oct(n)) # octal
print(hex(n)) # hexadecimal
print(int('101101', 2), int('55', 8), int('2D', 16))You can also write numbers directly in these bases: 0b1010 is 10, 0o17 is 15 and 0xFF is 255.
Summary & practice
- Base = number of digits: decimal 10, binary 2, octal 8, hexadecimal 16.
- Value = Σ digit × baseposition, position starting at 0 from the right.
- 1 octal digit = 3 bits; 1 hex digit = 4 bits.
- Python:
bin(),oct(),hex(),int(s, base).
1) Which of these is not a valid octal number: 175, 382, 66? 2) What is the decimal value of (1F)₁₆? 3) How many bits are needed to write the hex number 3A9?
Answers: 1) 382 (digit 8 is not allowed). 2) 31. 3) 3 hex digits × 4 = 12 bits.
