Binary Representation & Number Systems (Essential)
Binary, Decimal, and Hexadecimal
Read aloud is unavailable in this browser.
Speaking Multiple Languages of Numbers
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.
Positional Number Systems: The Universal Pattern
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:
- 5 × 10² (hundreds place) = 500
- 3 × 10¹ (tens place) = 30
- 2 × 10⁰ (ones place) = 2
- Total: 500 + 30 + 2 = 532
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): Our Native Tongue
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): The Computer's Native Language
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:
- All 1s = One less than a power of 2. Example: 11111111 = 255 = 2⁸ - 1
- Single 1 = A power of 2. Example: 10000000 = 128 = 2⁷
- Alternating = Useful patterns. Example: 10101010 = 170, 01010101 = 85
Binary Arithmetic Intuition:
- Adding 1 to a binary number flips the rightmost 0 to 1 (and carries if needed)
- Multiplying by 2 shifts all bits left by one position (adds a 0 on the right)
- Dividing by 2 shifts all bits right by one position (drops the rightmost bit)
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): The Human Bridge to Binary
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:
- Each hexadecimal digit represents exactly 4 binary digits (bits)
- One byte (8 bits) = exactly 2 hex digits
- Conversion between binary and hex is trivially easy — just group bits into fours
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.
Conversion Techniques: Practical Methods
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.
Why Hexadecimal Dominates in Programming
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:
- Binary: 11111111 (8 characters)
- Hex: FF (2 characters)
- Decimal: 255 (3 characters)
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.
- •Memory Addresses — 64-bit address: 0x00007FFEEF3B2A40. The structure of memory regions is visible in the hex digits.
- •Color Codes — #FF5733 = Red:255, Green:87, Blue:51. Each pair of hex digits is one color channel.
- •MAC Addresses — 00:1A:2B:3C:4D:5E. Six bytes, colon-separated for readability.
- •Unicode Code Points — U+1F600 (😀). The hex value maps directly to the character's position in the Unicode table.
- •Binary File Inspection — Hex editors show files as hex bytes, making patterns visible.
- •Debugging — Stack traces, register values, and memory dumps all use hex by convention.
- •Bitwise Operations — 0xFF masks eight bits, 0x0F masks four bits. The bit pattern is obvious.
- •Error Codes — Windows HRESULT, HTTP status extensions: 0x80070005 is more memorable than 2147942405.
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.
Common Values Worth Recognizing Instantly
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.
KEEP LEARNING
There’s more to understand.
You’re reading the introduction. Create a free account to read the complete lesson, save notes and keep track of your progress.