Number Systems Explained: Binary, Octal, Decimal, and Hexadecimal
What Is a Number System?
A number system is a way to represent quantities using a fixed set of symbols and rules. The system you use every day — decimal — uses ten digits (0–9) because humans have ten fingers. But computers don't have fingers. They have transistors, which understand only two states: on and off. That's why different number systems exist.
Every number system has three key components:
| Component | Definition | Decimal Example |
|---|---|---|
| Base (radix) | The number of unique digits | 10 |
| Digits | The symbols used to represent values | 0, 1, 2, ..., 9 |
| Place value | The weight of each position, calculated as base^position | 10^0, 10^1, 10^2 |
The same number looks different depending on the base. The value 255 in decimal is 11111111 in binary, 377 in octal, and FF in hexadecimal — but it represents the exact same quantity.
The Four Common Number Systems
Decimal (Base-10)
Decimal is the number system you already know. It uses digits 0 through 9, and each position represents a power of 10.
345 = 3 × 10² + 4 × 10¹ + 5 × 10⁰
= 300 + 40 + 5
= 345
You use decimal for everyday counting, money, and measurements. In programming, decimal is the default for human-readable output.
Binary (Base-2)
Binary uses only two digits: 0 and 1. Each position represents a power of 2. This is the native language of computers — every piece of data in memory is stored as binary.
1011 = 1 × 2³ + 0 × 2² + 1 × 2¹ + 1 × 2⁰
= 8 + 0 + 2 + 1
= 11 (decimal)
Common uses of binary in programming include bitmasks, permission flags, and low-level hardware control.
Octal (Base-8)
Octal uses digits 0 through 7. Each position represents a power of 8. Octal was popular in early computing when systems used 12-bit or 36-bit word sizes, because those divide evenly by 3.
72 = 7 × 8¹ + 2 × 8⁰
= 56 + 2
= 58 (decimal)
Today, octal appears most often in Unix file permissions. The command chmod 755 sets permissions using octal digits where each digit represents read, write, and execute bits for owner, group, and others.
Hexadecimal (Base-16)
Hexadecimal (hex) uses sixteen digits: 0–9 and A–F. The letters A through F represent decimal values 10 through 15. Each position represents a power of 16.
2F = 2 × 16¹ + 15 × 16⁰
= 32 + 15
= 47 (decimal)
Hex is the go-to format for representing binary data compactly. One hex digit maps exactly to four binary digits, making conversion between the two trivial.
Comparing the Systems
| System | Base | Digits | Typical Use |
|---|---|---|---|
| Binary | 2 | 0, 1 | Logic gates, bitmasks, low-level hardware |
| Octal | 8 | 0–7 | Unix file permissions |
| Decimal | 10 | 0–9 | Human-readable output, everyday math |
| Hexadecimal | 16 | 0–9, A–F | Memory addresses, color values, debugging |
Conversion Methods
Converting from Any Base to Decimal
Multiply each digit by its place value (base raised to the position power) and add the results. This works for every base.
Binary 1101 → Decimal:
1×2³ + 1×2² + 0×2¹ + 1×2⁰ = 8 + 4 + 0 + 1 = 13
Hex 3A → Decimal:
3×16¹ + 10×16⁰ = 48 + 10 = 58
Converting from Decimal to Any Base
Divide the decimal number by the target base repeatedly. Collect the remainders in reverse order.
Decimal 255 → Binary:
255 ÷ 2 = 127 remainder 1
127 ÷ 2 = 63 remainder 1
63 ÷ 2 = 31 remainder 1
31 ÷ 2 = 15 remainder 1
15 ÷ 2 = 7 remainder 1
7 ÷ 2 = 3 remainder 1
3 ÷ 2 = 1 remainder 1
1 ÷ 2 = 0 remainder 1
Result (read bottom-up): 11111111
Decimal 255 → Hex:
255 ÷ 16 = 15 remainder 15 (F)
15 ÷ 16 = 0 remainder 15 (F)
Result: FF
Binary ↔ Hex Shortcut
This is the conversion trick developers use most often. Group binary digits in sets of four (from right to left), then replace each group with its hex equivalent.
Binary: 1101 0110 0011
Hex: D 6 3
Result: D63
No long division needed — just memorize the 16 possible 4-bit patterns and their hex equivalents.
Number Systems in Programming
Bitmasks and Permissions
Many APIs use bit positions to represent flags. Understanding binary lets you read and set these values directly.
// Read permission = 4 (100), Write = 2 (010), Execute = 1 (001)
const READ_WRITE = 0b110; // 6
const ALL = 0b111; // 7
// Check if read is set
if (permissions & 0b100) {
console.log("Read access granted");
}
CSS Color Values
CSS colors like #FF5733 are hexadecimal. Each pair of hex digits represents the red, green, or blue channel from 0 to 255.
#FF5733
RR = FF = 255 (red)
GG = 57 = 87 (green)
BB = 33 = 51 (blue)
Memory Addresses
Debuggers and profilers display memory addresses in hex. Understanding hex helps you spot patterns and alignment issues in memory dumps.
// Typical memory address output
0x7fff5fbff8a0 // hex address
Key Takeaways
- Every number system uses a base, a set of digits, and place values based on powers of the base
- Binary is fundamental to computing; decimal is for humans; hex bridges the two
- Octal survives mainly in Unix file permissions
- The divide-by-base method converts decimal to any base; multiply-by-place-value converts any base to decimal
- The binary-to-hex shortcut (group 4 bits) is the most practical conversion for daily programming
- Hex appears everywhere: CSS colors, memory addresses, debug output, and byte representations
Try It Yourself
Ready to practice base conversions? Use our free Number Base Converter to convert between binary, octal, decimal, and hexadecimal instantly.