Listen to this article
Browser text-to-speech
Introduction to Number Systems
Every number you encounter daily uses the decimal system—base 10. You learned to count with ten fingers, so base 10 feels natural. But computers do not have fingers. They have transistors that are either on or off, which means they naturally work in base 2: binary.
Understanding binary, hexadecimal (base 16), and octal (base 8) is essential for programmers. These number systems appear everywhere in computing—from memory addresses to color codes, file permissions to bitwise operations.
Why Computers Use Binary (Base 2)
The Hardware Reality
At the lowest level, computers are built from billions of transistors. Each transistor can exist in one of two states: on (conducting electricity) or off (not conducting). This binary state maps perfectly to the digits 0 and 1.
Why not use 10 different voltage levels for base 10?
Theoretically possible, but practically problematic:
- Distinguishing between 10 voltage levels requires precise measurements
- Electrical noise could cause errors (is that voltage 4 or 5?)
- Binary is more reliable: any voltage above a threshold is 1, below is 0
Binary Representation
In binary, each digit position represents a power of 2:
| Position | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
|---|---|---|---|---|---|---|---|---|
| Value | 128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
For example, the binary number 10110101 equals:
- 1×128 + 0×64 + 1×32 + 1×16 + 0×8 + 1×4 + 0×2 + 1×1
- = 128 + 32 + 16 + 4 + 1
- = 181 in decimal
Bits and Bytes
A single binary digit is called a bit. Eight bits form a byte, which can represent values from 0 to 255 (2^8 - 1). Common groupings include:
- Nibble: 4 bits (0-15)
- Byte: 8 bits (0-255)
- Word: 16, 32, or 64 bits depending on processor architecture
Hexadecimal (Base 16): The Programmer's Favorite
Why Hexadecimal?
Binary is perfect for computers but terrible for humans. Reading 11111111101011001010111011110000 is exhausting and error-prone. Hexadecimal solves this by providing a compact representation.
Since 16 = 2^4, each hexadecimal digit represents exactly 4 binary digits (one nibble). This creates a perfect mapping:
| Decimal | Binary | Hex |
|---|---|---|
| 0 | 0000 | 0 |
| 1 | 0001 | 1 |
| 2 | 0010 | 2 |
| 3 | 0011 | 3 |
| 4 | 0100 | 4 |
| 5 | 0101 | 5 |
| 6 | 0110 | 6 |
| 7 | 0111 | 7 |
| 8 | 1000 | 8 |
| 9 | 1001 | 9 |
| 10 | 1010 | A |
| 11 | 1011 | B |
| 12 | 1100 | C |
| 13 | 1101 | D |
| 14 | 1110 | E |
| 15 | 1111 | F |
Hexadecimal Notation
Hex numbers are commonly prefixed to distinguish them from decimal:
- 0x prefix:
0xFF(C, JavaScript, Python) - # prefix:
#FF5733(CSS colors) - h suffix:
FFh(Assembly language)
Common Hex Use Cases
1. Memory Addresses
Memory addresses are almost always displayed in hex:
0x00007FFD4A2B3C40
This 64-bit address is much more readable than its binary equivalent (64 ones and zeros).
2. Color Codes
Web colors use hex RGB values:
#FF0000= Red (255 red, 0 green, 0 blue)#00FF00= Green#0000FF= Blue#FFFFFF= White (all channels at maximum)#000000= Black (all channels at zero)
Each pair of hex digits represents one color channel (0-255).
3. Byte Values
A single byte ranges from 00 to FF in hex (0-255 decimal). Two hex characters are easier to read than up to three decimal digits.
Octal (Base 8): The Historical System
Origins of Octal
Octal was popular in early computing for the same reason as hex—it maps cleanly to binary (8 = 2^3, so each octal digit represents 3 bits). Early computers like the PDP-8 used 12-bit words, which divide evenly by 3 but not by 4, making octal the natural choice.
Octal in Modern Computing
While mostly superseded by hex, octal survives in one important area: Unix file permissions.
chmod 755 script.sh
Each digit represents permissions for owner, group, and others:
- 7 = 111 binary = read + write + execute
- 5 = 101 binary = read + execute
- 0 = 000 binary = no permissions
The permission bits:
| Octal | Binary | Meaning |
|---|---|---|
| 0 | 000 | No permission |
| 1 | 001 | Execute only |
| 2 | 010 | Write only |
| 3 | 011 | Write + Execute |
| 4 | 100 | Read only |
| 5 | 101 | Read + Execute |
| 6 | 110 | Read + Write |
| 7 | 111 | Full permission |
Octal Notation
Octal numbers typically use:
- 0 prefix:
0755(C, JavaScript) - 0o prefix:
0o755(Python 3)
Converting Between Number Systems
Decimal to Binary
Method: Repeated Division by 2
To convert 181 to binary:
- 181 ÷ 2 = 90 remainder 1
- 90 ÷ 2 = 45 remainder 0
- 45 ÷ 2 = 22 remainder 1
- 22 ÷ 2 = 11 remainder 0
- 11 ÷ 2 = 5 remainder 1
- 5 ÷ 2 = 2 remainder 1
- 2 ÷ 2 = 1 remainder 0
- 1 ÷ 2 = 0 remainder 1
Reading remainders bottom-to-top: 10110101
Binary to Hexadecimal
Method: Group into Nibbles
Convert 10110101 to hex:
- Group into 4-bit sections from right:
1011 0101 - Convert each group:
1011= B,0101= 5 - Result: B5
Hexadecimal to Decimal
Method: Positional Notation
Convert B5 to decimal:
- B = 11, position 1: 11 × 16^1 = 176
- 5 = 5, position 0: 5 × 16^0 = 5
- Total: 176 + 5 = 181
Quick Reference Conversions Table
| Decimal | Binary | Octal | Hex |
|---|---|---|---|
| 0 | 0 | 0 | 0 |
| 8 | 1000 | 10 | 8 |
| 10 | 1010 | 12 | A |
| 15 | 1111 | 17 | F |
| 16 | 10000 | 20 | 10 |
| 32 | 100000 | 40 | 20 |
| 64 | 1000000 | 100 | 40 |
| 100 | 1100100 | 144 | 64 |
| 127 | 1111111 | 177 | 7F |
| 128 | 10000000 | 200 | 80 |
| 255 | 11111111 | 377 | FF |
| 256 | 100000000 | 400 | 100 |
Binary Arithmetic Basics
Binary Addition
Binary addition follows simple rules:
- 0 + 0 = 0
- 0 + 1 = 1
- 1 + 0 = 1
- 1 + 1 = 10 (0, carry 1)
Example: 1011 + 0110
1011 (11 in decimal)
+ 0110 (6 in decimal)
------
10001 (17 in decimal)
Binary Subtraction
Using two's complement (how computers actually subtract):
- Invert all bits of the subtrahend
- Add 1
- Add to the minuend
Example: 1011 - 0110 (11 - 6)
- Invert 0110 → 1001
- Add 1 → 1010
- Add: 1011 + 1010 = 10101
- Discard overflow: 0101 = 5
Bitwise Operations
Programmers frequently use bitwise operations:
AND (&): Both bits must be 1
1010 & 1100 = 1000
OR (|): Either bit can be 1
1010 | 1100 = 1110
XOR (^): Exactly one bit must be 1
1010 ^ 1100 = 0110
NOT (~): Flip all bits
~1010 = 0101
Left Shift (<<): Multiply by 2
1010 << 1 = 10100
Right Shift (>>): Divide by 2
1010 >> 1 = 0101
Practical Applications
Debugging Memory
When debugging, memory dumps display hex:
0x0000: 48 65 6C 6C 6F 20 57 6F Hello Wo
0x0008: 72 6C 64 21 00 00 00 00 rld!....
Each byte shown in hex, with ASCII interpretation on the right.
Network Protocols
MAC addresses use hex: 00:1A:2B:3C:4D:5E
IPv6 addresses use hex: 2001:0db8:85a3:0000:0000:8a2e:0370:7334
Cryptography
Hash values are displayed in hex:
SHA-256: e3b0c44298fc1c149afbf4c8996fb924...
Assembly and Low-Level Programming
Instruction opcodes, register values, and memory addresses all use hex for readability.
Key Takeaways
- Binary is the native language of computers because transistors have two states
- Hexadecimal provides human-readable binary with a perfect 4:1 bit mapping
- Octal survives primarily in Unix permissions due to the 3-bit grouping
- Converting between bases becomes intuitive with practice
- Bitwise operations are powerful tools for efficient programming
See what our calculators can do for you
Ready to take control of your finances?
Use our free converters to switch between binary, hex, decimal, and octal
Explore Our ToolsFrequently Asked Questions
Common questions about the Binary, Hex, and Octal: Number Systems Every Programmer Should Know
