Loading content...
Computers think in binary. Humans think in decimal. Neither is ideal for debugging memory dumps, reading color codes, or examining byte sequences. That's where hexadecimal enters the picture.
As a programmer or computer scientist, you need fluency in all three number systems. This isn't about performing tedious manual conversions — modern tools handle that. It's about recognizing patterns, debugging efficiently, and understanding what your tools are showing you.
When you see "0xFF" in a debugger, you should instantly know that's 255. When memory shows "41 42 43," you should recognize that's "ABC." When a color is specified as "#FF5733," you should see the RGB values immediately. This fluency comes from understanding how these number systems work and relate to each other.
By the end of this page, you will understand positional number systems deeply — why they work the way they do, not just how to convert between them. You'll develop intuition for binary, decimal, and hexadecimal, and understand why hex is the preferred way for humans to view binary data.
All the number systems we use in computing share a fundamental principle: they are positional systems. In a positional system, each digit's value depends on both the digit itself and its position within the number.
The General Formula:
For any number in base b with digits dₙdₙ₋₁...d₁d₀, the value is:
Value = dₙ × bⁿ + dₙ₋₁ × bⁿ⁻¹ + ... + d₁ × b¹ + d₀ × b⁰
Each position represents a successive power of the base, starting from b⁰ = 1 on the right.
Example in Decimal (Base 10):
The number 532 means:
You've been using this system since childhood — so intuitively that you don't think about it. But this same principle applies to any base.
| Position | Decimal (Base 10) | Binary (Base 2) | Hexadecimal (Base 16) |
|---|---|---|---|
| 0 (rightmost) | 10⁰ = 1 | 2⁰ = 1 | 16⁰ = 1 |
| 1 | 10¹ = 10 | 2¹ = 2 | 16¹ = 16 |
| 2 | 10² = 100 | 2² = 4 | 16² = 256 |
| 3 | 10³ = 1,000 | 2³ = 8 | 16³ = 4,096 |
| 4 | 10⁴ = 10,000 | 2⁴ = 16 | 16⁴ = 65,536 |
| 5 | 10⁵ = 100,000 | 2⁵ = 32 | 16⁵ = 1,048,576 |
In any base b, you need exactly b different symbols for digits (0 through b-1). Decimal uses 0-9. Binary uses 0-1. Hexadecimal uses 0-9 plus A-F (where A=10, B=11, C=12, D=13, E=14, F=15). The digit symbols are arbitrary; their positions give them meaning.
Decimal (base 10) is humanity's default number system. It uses ten digits: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9.
Why Base 10?
The prevailing theory is that we adopted base 10 because humans have 10 fingers. Ancient counting systems were based on body parts, and 10 was anatomically convenient. Not all cultures used base 10 — the Maya used base 20 (fingers and toes), and the Babylonians used base 60 (which is why we have 60 seconds in a minute).
Decimal Is Arbitrary for Computers:
From a computer's perspective, there's nothing special about base 10. Computers don't have fingers. The electrical circuits that perform computation don't care whether we humans conceptualize values in base 10, base 2, or any other base. Binary was chosen for engineering reasons (reliability, simplicity), not mathematical ones.
The Human-Computer Interface:
Every time you type a number like "42" or see a result like "1024," a conversion is happening. The computer stores and manipulates values in binary; we interface with them in decimal. This conversion is so seamless that we rarely notice it, but it's happening in every keyboard press and every screen pixel.
Think of decimal as one 'language' for expressing quantities. The quantity itself (the abstract count of things) exists independently. Whether we call it '42' (decimal), '101010' (binary), or '2A' (hexadecimal), we're describing the same underlying quantity in different notation systems.
Binary (base 2) uses only two digits: 0 and 1. As we explored earlier, this maps directly to the physical states of electronic circuits.
Reading Binary Numbers:
Each position in a binary number represents a power of 2, from right to left:
Position: 7 6 5 4 3 2 1 0 Value: 128 64 32 16 8 4 2 1
Example: Converting 10110101 to Decimal:
Position: 7 6 5 4 3 2 1 0
Binary: 1 0 1 1 0 1 0 1
Value: 128 0 32 16 0 4 0 1
Sum: 128 + 32 + 16 + 4 + 1 = 181
The binary representation essentially answers: "Which powers of 2 do we add together to get this number?"
| Decimal | Binary | Decimal | Binary |
|---|---|---|---|
| 0 | 0000 | 8 | 1000 |
| 1 | 0001 | 9 | 1001 |
| 2 | 0010 | 10 | 1010 |
| 3 | 0011 | 11 | 1011 |
| 4 | 0100 | 12 | 1100 |
| 5 | 0101 | 13 | 1101 |
| 6 | 0110 | 14 | 1110 |
| 7 | 0111 | 15 | 1111 |
Recognizing Binary Patterns:
Some binary patterns are worth memorizing:
Binary Arithmetic Intuition:
In many programming languages, binary numbers are written with a '0b' prefix: 0b10110101. This distinguishes them from decimal. Without context, '101' could mean one hundred one (decimal) or five (binary). The prefix removes ambiguity.
Hexadecimal (base 16) uses sixteen digits: 0-9 and A-F (where A=10, B=11, C=12, D=13, E=14, F=15).
Why Hexadecimal?
Here's the key insight: 16 = 2⁴. This means:
Hexadecimal gives us the compactness of a higher base while maintaining perfect alignment with binary. It's the ideal human-readable format for binary data.
The Hex Digit Mapping:
| Hex | Decimal | Binary | Hex | Decimal | Binary |
|---|---|---|---|---|---|
| 0 | 0 | 0000 | 8 | 8 | 1000 |
| 1 | 1 | 0001 | 9 | 9 | 1001 |
| 2 | 2 | 0010 | A | 10 | 1010 |
| 3 | 3 | 0011 | B | 11 | 1011 |
| 4 | 4 | 0100 | C | 12 | 1100 |
| 5 | 5 | 0101 | D | 13 | 1101 |
| 6 | 6 | 0110 | E | 14 | 1110 |
| 7 | 7 | 0111 | F | 15 | 1111 |
Converting Binary to Hex:
Group binary digits into sets of 4 (from the right), then convert each group:
Binary: 1011 0101
Hex: B 5
Result: 0xB5 = 181
Converting Hex to Binary:
Expand each hex digit into 4 binary digits:
Hex: 2 A F E
Binary: 0010 1010 1111 1110
Result: 0010101011111110 = 10,990
This conversion is so mechanical that experienced programmers do it instantly by sight.
Hexadecimal numbers use various prefixes depending on context: '0x' in C/C++/Java/JavaScript (0xFF), '$' in assembly and some older languages ($FF), and '#' for HTML/CSS colors (#FF5733). The meaning is the same — only the notation differs.
Let's build practical conversion skills. While you'll rarely convert manually in practice (calculators and programming languages handle it), understanding the process builds deep intuition.
Decimal to Binary — The Division Method:
Repeatedly divide by 2, recording remainders. Read remainders from bottom to top.
Example: Convert 45 to binary
45 ÷ 2 = 22 remainder 1 ↑
22 ÷ 2 = 11 remainder 0 |
11 ÷ 2 = 5 remainder 1 |
5 ÷ 2 = 2 remainder 1 | Read upward
2 ÷ 2 = 1 remainder 0 |
1 ÷ 2 = 0 remainder 1 |
Result: 101101 = 45
Decimal to Binary — The Power Subtraction Method:
Find the largest power of 2 that fits, subtract, repeat.
Example: Convert 45 to binary
45 >= 32 (2⁵)? Yes → bit 5 = 1, remainder = 45 - 32 = 13
13 >= 16 (2⁴)? No → bit 4 = 0
13 >= 8 (2³)? Yes → bit 3 = 1, remainder = 13 - 8 = 5
5 >= 4 (2²)? Yes → bit 2 = 1, remainder = 5 - 4 = 1
1 >= 2 (2¹)? No → bit 1 = 0
1 >= 1 (2⁰)? Yes → bit 0 = 1, remainder = 1 - 1 = 0
Result: 101101 = 45
Decimal to Hexadecimal — The Division Method:
Same as binary, but divide by 16:
Example: Convert 255 to hexadecimal
255 ÷ 16 = 15 remainder 15 (F) ↑
15 ÷ 16 = 0 remainder 15 (F) |
Result: 0xFF = 255
Alternative: Decimal → Binary → Hex:
Often easier for humans: first convert to binary, then group into fours:
255 → 11111111 → 1111 1111 → F F → 0xFF
You don't need to be fast at manual conversion — calculators exist. But you should understand the process well enough that common values (0xFF = 255, 0x100 = 256, 0x10 = 16) are instantly recognizable. This fluency develops naturally with exposure.
Hexadecimal appears everywhere in computing: debuggers, memory dumps, color codes, network addresses, character encodings, and more. Here's why it won this role:
Compactness Without Loss:
Binary is the truth but unwieldy. The byte 255 is:
Hex is the most compact while retaining perfect binary alignment.
Byte-Aligned Representation:
Since 1 byte = 8 bits = 2 hex digits, byte boundaries are always visible in hex. The 32-bit value 0x12345678 clearly shows four bytes: 12, 34, 56, 78. In decimal (305,419,896), the byte structure is invisible.
Never forget: hexadecimal is a convenience for human readers. Computers always work in binary internally. Hex is simply a more compact and structured way for us to view and communicate binary data. When you write 0xFF in code, the compiler converts it to binary — you're just making the source code more readable.
Some values appear so frequently in computing that recognizing them instantly is essential. These aren't arbitrary — each has structural significance:
Values You'll See Constantly:
| Decimal | Hex | Binary (8-bit) | Significance |
|---|---|---|---|
| 0 | 0x00 | 00000000 | Null, false, empty, beginning |
| 1 | 0x01 | 00000001 | True, single increment |
| 127 | 0x7F | 01111111 | Maximum signed 8-bit value |
| 128 | 0x80 | 10000000 | Minimum signed 8-bit value (as negative) |
| 255 | 0xFF | 11111111 | Maximum unsigned 8-bit value |
| 256 | 0x100 | 100000000 (9 bits) | First value needing more than 8 bits |
| 32,767 | 0x7FFF | — | Maximum signed 16-bit value |
| 65,535 | 0xFFFF | — | Maximum unsigned 16-bit value |
| 2,147,483,647 | 0x7FFFFFFF | — | Maximum signed 32-bit value |
| 4,294,967,295 | 0xFFFFFFFF | — | Maximum unsigned 32-bit value |
Powers of Two:
2⁰ = 1 2⁸ = 256 2¹⁶ = 65,536
2¹ = 2 2⁹ = 512 2²⁰ = 1,048,576 (≈ 1 million)
2² = 4 2¹⁰ = 1,024 2³⁰ = 1,073,741,824 (≈ 1 billion)
2³ = 8 2¹¹ = 2,048 2³² = 4,294,967,296 (≈ 4 billion)
2⁴ = 16 2¹² = 4,096 2⁴⁰ = 1,099,511,627,776 (≈ 1 trillion)
2⁵ = 32 2¹³ = 8,192 2⁶⁴ = 18,446,744,073,709,551,616
2⁶ = 64 2¹⁴ = 16,384
2⁷ = 128 2¹⁵ = 32,768
Knowing that 2¹⁰ ≈ 1,000 (it's 1,024) gives you quick mental math: 2²⁰ ≈ 1 million, 2³⁰ ≈ 1 billion, 2⁴⁰ ≈ 1 trillion.
In signed representation, 0x7F (127) is the maximum positive value for a signed byte, and 0x80 (128) represents -128. The leading bit (0 or 1) indicates sign. This is why signed bytes range from -128 to 127, not -128 to 128 — there are 256 values total, but one is used for zero.
Before hexadecimal became dominant, octal (base 8) was popular. It uses digits 0-7, with each octal digit representing exactly 3 binary bits (since 8 = 2³).
Where Octal Still Appears:
Octal isn't dead — it survives in specific contexts:
The Problem with Octal:
Hexadecimal's perfect byte alignment ultimately made it the standard.
In C/C++/Java, a leading zero creates an octal literal: int x = 010 means x = 8, not 10! This is a common source of bugs when padding numbers with zeros. Many modern languages (Python 3, JavaScript ES6+) require explicit 0o prefix for octal to prevent this confusion.
Let's apply our understanding to real-world examples. These scenarios demonstrate why number system fluency matters in practice.
Example 1: Color Codes
The CSS color #3498DB breaks down as:
Red: 0x34 = 52 (20% brightness)
Green: 0x98 = 152 (60% brightness)
Blue: 0xDB = 219 (86% brightness)
This is a blue-ish color, which you can verify by the high blue component.
Example 2: ASCII Text
A hex editor shows: 48 65 6C 6C 6F
0x48 = 72 = 'H'
0x65 = 101 = 'e'
0x6C = 108 = 'l'
0x6C = 108 = 'l'
0x6F = 111 = 'o'
The data spells "Hello".
Example 3: Memory Address
Debugger shows: 0x00007FF7A1B23C40
Example 4: Unix Permissions
chmod 644 means:
6 = 110 (binary) = rw- (read + write)
4 = 100 (binary) = r-- (read only)
4 = 100 (binary) = r-- (read only)
Owner can read and write; group and others can only read.
Example 5: Network Packet
IP header shows: 45 00 00 3C ...
0x45 = 0100 0101
└──┬───┘
Version (4) + Header Length (5 x 4 = 20 bytes)
The first nibble (4) indicates IPv4; the second (5) indicates 20-byte header.
You don't need to memorize ASCII tables or protocol formats. With practice, you'll start recognizing patterns: 0x20 is space, 0x41-0x5A are uppercase letters, 0x61-0x7A are lowercase. The patterns emerge naturally from exposure.
We've explored the three number systems essential to computing. Let's consolidate the key insights:
What's Next:
We've now established how data is represented at the bit level and how humans view that data through different number systems. The final page in this module connects everything: how data is ultimately stored as bits — the physical reality that underlies every data structure and algorithm you'll ever encounter.
You now understand binary, decimal, and hexadecimal number systems deeply. You know why hex became the standard for human-readable binary data, and you have practical techniques for conversion and interpretation. Next, we'll explore how data is physically stored as bits in computer memory.