Number Systems: Understanding Binary, Hexadecimal, and Octal
How binary, hexadecimal, and octal work, how to convert between them, and where programmers meet them every day, from bitwise operations to Unix permissions.
Every number you see on a screen is stored as binary in your computer's memory. Understanding binary, hexadecimal, and octal helps you work with low-level code, debug memory issues, and understand how computers actually work.
The Basics: What is a Number System?
A number system (or base) defines how we represent values using digits. The base tells you how many unique digits are available.
| System | Base | Digits Used | Example |
|---|---|---|---|
| Binary | 2 | 0, 1 | 1010 |
| Octal | 8 | 0-7 | 12 |
| Decimal | 10 | 0-9 | 10 |
| Hexadecimal | 16 | 0-9, A-F | A |
All of these represent the same value: ten in decimal.
Binary (Base-2): The Language of Computers
Binary uses only two digits: 0 and 1. Each digit is called a bit (binary digit). This matches how computers work: transistors are either on (1) or off (0).
How Binary Counting Works
In decimal, each position is worth 10× the position to its right. In binary, each position is worth 2× the position to its right.
Position values (right to left):
... 128 64 32 16 8 4 2 1
2⁷ 2⁶ 2⁵ 2⁴ 2³ 2² 2¹ 2⁰
Converting Binary to Decimal
Add up the position values where there's a 1:
Binary: 1 0 1 1 0 1 0 1
│ │ │ │ │ │ │ └─ 1 × 1 = 1
│ │ │ │ │ │ └─── 0 × 2 = 0
│ │ │ │ │ └───── 1 × 4 = 4
│ │ │ │ └─────── 0 × 8 = 0
│ │ │ └───────── 1 × 16 = 16
│ │ └─────────── 1 × 32 = 32
│ └───────────── 0 × 64 = 0
└─────────────── 1 × 128 = 128
─────
Total: 181
So 10110101 in binary = 181 in decimal.
Converting Decimal to Binary
Repeatedly divide by 2 and track the remainders:
Convert 45 to binary:
45 ÷ 2 = 22 remainder 1 ↑
22 ÷ 2 = 11 remainder 0 │
11 ÷ 2 = 5 remainder 1 │ Read upward
5 ÷ 2 = 2 remainder 1 │
2 ÷ 2 = 1 remainder 0 │
1 ÷ 2 = 0 remainder 1 │
Result: 101101
Common Binary Values
| Decimal | Binary | Notes |
|---|---|---|
| 0 | 0 | Zero |
| 1 | 1 | One |
| 2 | 10 | First "carry" |
| 7 | 111 | 3 bits max |
| 8 | 1000 | 4 bits needed |
| 15 | 1111 | 4 bits max (nibble) |
| 16 | 10000 | 5 bits needed |
| 255 | 11111111 | 8 bits max (byte) |
| 256 | 100000000 | 9 bits needed |
Binary Arithmetic
Addition: Subtraction:
1011 (11) 1011 (11)
+ 0101 (5) - 0101 (5)
────── ──────
10000 (16) 0110 (6)
Hexadecimal (Base-16): Compact Binary
Hexadecimal (hex) uses 16 digits. Since we only have 10 numeric digits (0-9), we use letters A-F for values 10-15.
| Decimal | Hex | Binary |
|---|---|---|
| 0 | 0 | 0000 |
| 1 | 1 | 0001 |
| 2 | 2 | 0010 |
| 3 | 3 | 0011 |
| 4 | 4 | 0100 |
| 5 | 5 | 0101 |
| 6 | 6 | 0110 |
| 7 | 7 | 0111 |
| 8 | 8 | 1000 |
| 9 | 9 | 1001 |
| 10 | A | 1010 |
| 11 | B | 1011 |
| 12 | C | 1100 |
| 13 | D | 1101 |
| 14 | E | 1110 |
| 15 | F | 1111 |
Why Hex is Perfect for Binary
One hex digit represents exactly 4 binary digits (a nibble). This makes conversion trivial:
Binary: 1111 1010 0011 1100
Hex: F A 3 C
So 1111101000111100 = 0xFA3C
This is why hex is everywhere in computing. It's a human-readable shorthand for binary.
Converting Hex to Decimal
Each position is worth 16× the position to its right:
Hex: 2 A F
│ │ └─ F (15) × 1 = 15
│ └─── A (10) × 16 = 160
└───── 2 × 256 = 512
─────
Total: 687
Common Uses for Hexadecimal
Colors (CSS/HTML):
color: #FF5733; /* RGB: 255, 87, 51 */
color: #FF5733CC; /* With alpha: 80% opacity */
Each pair represents a color channel (00-FF = 0-255):
#FF5733
││││││
││││└┴─ Blue: 0x33 = 51
││└┴─── Green: 0x57 = 87
└┴───── Red: 0xFF = 255
Memory Addresses:
0x7FFF5FBFF8C0 (Stack address)
0xFFFFFFFF81000000 (Kernel address on x86-64 Linux)
Character Codes:
'A' = 0x41 = 65
'a' = 0x61 = 97
' ' = 0x20 = 32
MAC Addresses:
00:1A:2B:3C:4D:5E
Octal (Base-8): Unix Permissions
Octal uses digits 0-7. One octal digit represents exactly 3 binary digits.
Binary: 111 101 101
Octal: 7 5 5
So 111101101 = 755 in octal
Unix File Permissions
This is where you'll encounter octal most often:
chmod 755 script.sh
Each digit represents permissions for owner, group, and others:
Permission bits:
r (read) = 4 = 100
w (write) = 2 = 010
x (execute) = 1 = 001
Example: 755
7 = rwx (4+2+1) = owner can read, write, execute
5 = r-x (4+0+1) = group can read and execute
5 = r-x (4+0+1) = others can read and execute
Common Permission Values
| Octal | Binary | Meaning | Symbolic |
|---|---|---|---|
| 0 | 000 | No permissions | --- |
| 1 | 001 | Execute only | --x |
| 4 | 100 | Read only | r-- |
| 5 | 101 | Read + execute | r-x |
| 6 | 110 | Read + write | rw- |
| 7 | 111 | Full permissions | rwx |
| Mode | Use Case |
|---|---|
| 755 | Executable scripts |
| 644 | Regular files |
| 700 | Private directories |
| 777 | Open to everyone (avoid!) |
Number Prefixes in Code
Different languages use prefixes to indicate the base:
// JavaScript
let binary = 0b1010; // 10 in decimal
let octal = 0o755; // 493 in decimal
let hex = 0xFF; // 255 in decimal
# Python
binary = 0b1010 # 10
octal = 0o755 # 493
hex_num = 0xFF # 255
// C / C++
int binary = 0b1010; // 10 (C++14+)
int octal = 0755; // 493 (leading zero!)
int hex = 0xFF; // 255
In C and C++, a leading zero means octal, which catches people out:
int x = 010; // This is 8, not 10!
Bitwise Operations
Understanding binary makes bitwise operations intuitive:
AND (&): Both bits must be 1
1010 (10)
& 1100 (12)
──────
1000 (8)
OR (|): Either bit can be 1
1010 (10)
| 1100 (12)
──────
1110 (14)
XOR (^): Bits must be different
1010 (10)
^ 1100 (12)
──────
0110 (6)
Left Shift (<<): Multiply by 2
0001 << 1 = 0010 (1 becomes 2)
0001 << 3 = 1000 (1 becomes 8)
Right Shift (>>): Divide by 2
1000 >> 1 = 0100 (8 becomes 4)
1000 >> 2 = 0010 (8 becomes 2)
Practical Uses
// Check if number is even
if ((n & 1) === 0) { /* even */ }
// Check if bit is set
if (flags & 0b0100) { /* bit 2 is set */ }
// Set a bit
flags = flags | 0b0100;
// Clear a bit
flags = flags & ~0b0100;
// Toggle a bit
flags = flags ^ 0b0100;
Quick Reference Table
| Decimal | Binary | Hex | Octal |
|---|---|---|---|
| 0 | 0000 0000 | 00 | 000 |
| 1 | 0000 0001 | 01 | 001 |
| 7 | 0000 0111 | 07 | 007 |
| 8 | 0000 1000 | 08 | 010 |
| 10 | 0000 1010 | 0A | 012 |
| 15 | 0000 1111 | 0F | 017 |
| 16 | 0001 0000 | 10 | 020 |
| 32 | 0010 0000 | 20 | 040 |
| 64 | 0100 0000 | 40 | 100 |
| 100 | 0110 0100 | 64 | 144 |
| 127 | 0111 1111 | 7F | 177 |
| 128 | 1000 0000 | 80 | 200 |
| 255 | 1111 1111 | FF | 377 |
| 256 | 1 0000 0000 | 100 | 400 |
Key Takeaways
- Binary (base-2): The native language of computers, 0s and 1s
- Hex (base-16): Compact binary representation, 1 hex digit = 4 bits
- Octal (base-8): Mainly used for Unix permissions, 1 octal digit = 3 bits
- Prefixes: 0b (binary), 0o (octal), 0x (hex)
- Colors: Hex codes are RGB values in hexadecimal
- Permissions: chmod uses octal (755, 644, etc.)
The Loopaloo Number Base Converter is a good place to practice. Type a number in one base to see it in binary, octal, decimal, and hex at once, or switch to its bitwise mode to check AND, OR, XOR, and shifts against your own working.