Introduction
Welcome to the most comprehensive guide to binary and hexadecimal number systems. Whether you're a computer science student, programmer, or curious learner, this guide will take you from the basics to advanced concepts with clear explanations and practical examples.
Number systems are the foundation of how computers represent and process data. While humans naturally think in decimal (base 10), computers operate in binary (base 2). Hexadecimal (base 16) serves as a convenient bridge between these two worlds, making complex binary data human-readable.
This guide covers number system fundamentals, binary and hexadecimal concepts, conversion techniques between all bases, real-world applications in computing, and plenty of worked examples to solidify your understanding.
Number Systems Overview
A number system (or numeral system) is a writing system for expressing numbers using digits or symbols in a logical manner. The base (or radix) of a number system determines how many unique digits it uses.
Binary
Octal
Decimal
Hexadecimal
Positional Number System
All these systems are positional, meaning the value of a digit depends on its position. Each position represents a power of the base:
Understanding Binary
Binary (base 2) is the fundamental language of computers. It uses only two digits: 0 and 1, representing off and on states in electronic circuits. Every piece of data in a computer—text, images, videos, programs—is ultimately stored as binary.
Why Binary?
Computers use binary because electronic circuits have two natural states:
- 0 = Low voltage / Off state
- 1 = High voltage / On state
This simplicity makes binary incredibly reliable and easy to implement in hardware. Transistors, the building blocks of modern computers, naturally operate in these two states.
Binary Digits (Bits)
A single binary digit is called a bit. Bits are grouped for convenience:
- 1 bit = 0 or 1 (2 possible values)
- 4 bits = nibble (16 possible values)
- 8 bits = byte (256 possible values)
- 16 bits = word (65,536 possible values)
- 32 bits = double word (~4.3 billion values)
- 64 bits = quad word (~18 quintillion values)
The term "bit" was coined by John Tukey in 1946 as a contraction of "binary digit". The word "byte" was coined by Werner Buchholz in 1956.
Binary Counting
Let's see how counting works in binary:
| Decimal | Binary | Pattern |
|---|---|---|
| 0 | 0000 | Start |
| 1 | 0001 | +1 |
| 2 | 0010 | Carry |
| 3 | 0011 | +1 |
| 4 | 0100 | Carry |
| 5 | 0101 | +1 |
| 6 | 0110 | Carry |
| 7 | 0111 | +1 |
| 8 | 1000 | Carry |
| 9 | 1001 | +1 |
| 10 | 1010 | Carry |
| 15 | 1111 | Max 4-bit |
| 16 | 10000 | 5 bits needed |
God made the integers; all else is the work of man. But even the integers had to wait for binary to find their true home in the digital world.
Understanding Hexadecimal
Hexadecimal (base 16, often shortened to "hex") is a number system that uses sixteen distinct symbols: 0-9 to represent values zero to nine, and A-F (or a-f) to represent values ten to fifteen.
Hexadecimal Digits
| Hex | Decimal | 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 |
| A | 10 | 1010 |
| B | 11 | 1011 |
| C | 12 | 1100 |
| D | 13 | 1101 |
| E | 14 | 1110 |
| F | 15 | 1111 |
Notation Conventions
Hexadecimal numbers can be written in several ways to distinguish them from decimal:
- 0x prefix:
0xFF(common in programming) - h suffix:
FFh(assembly language) - Subscript:
FF₁₆(mathematical notation) - # prefix:
#FF(HTML/CSS colors)
Hexadecimal is case-insensitive: 0xFF, 0xff, and 0xFf all represent the same value (255 in decimal).
Why Use Hexadecimal?
If computers use binary, why do programmers prefer hexadecimal? The answer lies in human readability and efficiency.
The Problem with Binary
Binary numbers get very long very quickly:
The Magic of 4:1 Ratio
Since 16 = 2⁴, each hexadecimal digit corresponds to exactly 4 binary bits. This creates a perfect mapping:
- 1 hex digit = 4 bits (a nibble)
- 2 hex digits = 8 bits (1 byte)
- 4 hex digits = 16 bits (1 word)
- 8 hex digits = 32 bits (1 double word)
Hexadecimal provides the perfect balance: compact enough for humans to read, yet directly convertible to binary for computers to process.
Real-World Uses of Hexadecimal
- Color codes: HTML/CSS colors like
#FF5733 - Memory addresses:
0x7FFF5FBFF8C0 - MAC addresses:
00:1A:2B:3C:4D:5E - Character encoding: ASCII values like
0x41= 'A' - Error codes:
0x80070005(Access Denied) - Unicode:
U+1F600(😀 emoji)
Binary ↔ Hexadecimal Conversion
Converting between binary and hexadecimal is straightforward thanks to the 4:1 relationship. Each hex digit maps to exactly 4 binary bits.
Binary to Hexadecimal
Method: Group binary digits into sets of 4 (starting from the right), then convert each group to its hex equivalent.
1101 0111 0101
1101 = D
0111 = 7
0101 = 5
Hexadecimal to Binary
Method: Convert each hex digit to its 4-bit binary equivalent.
3 = 0011
F = 1111
2 = 0010
A = 1010
0011 1111 0010 1010
When converting binary to hex, if the leftmost group has fewer than 4 bits, pad it with leading zeros. For example, 10110₂ becomes 0001 0110 = 16₁₆.
Binary ↔ Decimal Conversion
Converting between binary and decimal requires understanding positional values.
Binary to Decimal
Method: Multiply each bit by its positional value (power of 2) and sum the results.
1×2⁷ + 0×2⁶ + 1×2⁵ + 1×2⁴ + 0×2³ + 1×2² + 0×2¹ + 1×2⁰
128 + 0 + 32 + 16 + 0 + 4 + 0 + 1
Decimal to Binary
Method: Repeatedly divide by 2 and record remainders (read bottom to top).
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
Hexadecimal ↔ Decimal Conversion
Hexadecimal to decimal conversion follows the same positional principle as binary, but with powers of 16.
Hexadecimal to Decimal
Method: Multiply each hex digit by its positional value (power of 16) and sum.
2×16² + A×16¹ + F×16⁰
= 2×256 + 10×16 + 15×1
512 + 160 + 15
Decimal to Hexadecimal
Method: Repeatedly divide by 16 and record remainders.
42 ÷ 16 = 2 remainder 10 (A)
2 ÷ 16 = 0 remainder 2
Decimal → Binary/Hex Conversion
Converting from decimal to binary or hex can be done directly or through an intermediate step.
Direct Method
Use repeated division by the target base (2 for binary, 16 for hex).
Shortcut Method: Decimal → Hex → Binary
Sometimes it's easier to convert decimal to hex first, then hex to binary:
4095 ÷ 16 = 255 remainder 15 (F)
255 ÷ 16 = 15 remainder 15 (F)
15 ÷ 16 = 0 remainder 15 (F)
So 4095₁₀ = FFF₁₆
F = 1111
F = 1111
F = 1111
Memorize powers of 2 up to 2¹⁶ (65536). This makes mental conversions much faster: 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768, 65536.
Real-World Applications
Binary and hexadecimal aren't just academic concepts—they're used everywhere in modern computing and technology.
1. Web Development
Hexadecimal colors are fundamental to web design:
2. Memory Addresses
Computer memory addresses are typically shown in hexadecimal:
3. Character Encoding
ASCII and Unicode use hexadecimal values:
| Character | ASCII (Hex) | ASCII (Decimal) | ASCII (Binary) |
|---|---|---|---|
| 'A' | 0x41 | 65 | 01000001 |
| 'a' | 0x61 | 97 | 01100001 |
| '0' | 0x30 | 48 | 00110000 |
| ' ' | 0x20 | 32 | 00100000 |
| '!' | 0x21 | 33 | 00100001 |
4. Network Addresses
MAC addresses use hexadecimal notation:
5. Error Codes
Many systems use hexadecimal error codes:
- Windows:
0x80070005(Access Denied) - HTTP:
0x1F4(Status 500) - Linux: Exit codes in hex
6. Unicode Emojis
Emojis are represented by Unicode code points in hex:
| Emoji | Unicode | HTML Entity |
|---|---|---|
| 😀 | U+1F600 | 😀 |
| ❤️ | U+2764 | ❤ |
| 🎉 | U+1F389 | 🎉 |
| 🚀 | U+1F680 | 🚀 |
Practice Examples
Let's work through some comprehensive examples to reinforce your understanding.
Example 1: IP Address Conversion
192 = 11000000
168 = 10101000
1 = 00000001
1 = 00000001
11000000.10101000.00000001.00000001
11000000 = C0
10101000 = A8
00000001 = 01
00000001 = 01
Example 2: Color Code Conversion
Red: 4A
Green: 90
Blue: E2
4A₁₆ = 4×16 + 10 = 74
90₁₆ = 9×16 + 0 = 144
E2₁₆ = 14×16 + 2 = 226
Example 3: Large Number Conversion
1000000 ÷ 16 = 62500 remainder 0
62500 ÷ 16 = 3906 remainder 4
3906 ÷ 16 = 244 remainder 2
244 ÷ 16 = 15 remainder 4
15 ÷ 16 = 0 remainder 15 (F)
F = 1111
4 = 0100
2 = 0010
4 = 0100
0 = 0000
You can verify: 11110100001001000000₂ = 2¹⁹ + 2¹⁸ + 2¹⁷ + 2¹⁶ + 2¹⁴ + 2⁹ + 2⁶ = 524288 + 262144 + 131072 + 65536 + 16384 + 512 + 64 = 1,000,000 ✓
Essential Tools & Calculators
Practice and master number system conversions with these helpful tools:
Online Resources
- RapidTables - Binary/Hex/Decimal converters
- Calculator.net - Number base conversions
- GeeksforGeeks - Programming tutorials
- Khan Academy - Computer science courses
Practice Problems
Try these conversion problems to test your skills:
- Convert
10110110₂to hexadecimal - Convert
0xDEADBEEF₁₆to binary - Convert
255₁₀to binary and hex - Convert
0xFF₁₆to decimal - Convert
1111111111111111₂to hex and decimal
1) B6₁₆, 2) 1101111010101101101111101111₂, 3) 11111111₂ = FF₁₆, 4) 255₁₀, 5) FFFF₁₆ = 65535₁₀
Conclusion
Binary and hexadecimal number systems are fundamental to computer science and programming. While they may seem abstract at first, mastering these concepts opens up a deeper understanding of how computers work and how data is represented.
Key Takeaways
- Binary (base 2) is the language of computers, using only 0s and 1s
- Hexadecimal (base 16) provides a human-friendly way to represent binary data
- 4:1 ratio - Each hex digit represents exactly 4 binary bits
- Conversions are straightforward with practice
- Applications are everywhere: colors, addresses, encoding, error codes
Next Steps
- Practice conversions - Work through examples daily
- Use calculators - Verify your manual calculations
- Explore programming - See these concepts in action
- Study computer architecture - Understand why binary matters
- Learn about encoding - ASCII, Unicode, and more
The best way to learn number systems is not to memorize tables, but to understand the underlying principles. Once you grasp the concept of positional notation, all bases become intuitive.
Thank you for reading this comprehensive guide to binary and hexadecimal number systems. We hope it has demystified these concepts and given you the confidence to work with different number bases in your programming and computing journey.