Binary & Hexadecimal: The Complete Guide

Master number systems, conversions, and their applications in computing

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.

2
Binary Base
16
Hex Base
4:1
Binary:Hex Ratio
Applications

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.

What You'll Learn

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

Base 2
0, 1
The language of computers. Every digital operation is ultimately reduced to combinations of 0s and 1s.

Octal

Base 8
0-7
Used in early computing systems. Each octal digit represents 3 binary bits. Still used in Unix file permissions.

Decimal

Base 10
0-9
The human standard. Based on our 10 fingers, this is the number system we use in everyday life.

Hexadecimal

Base 16
0-9, A-F
The programmer's friend. Each hex digit represents 4 binary bits, making it perfect for representing bytes.

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:

// Decimal example: 345 345 = 3×10² + 4×10¹ + 5×10⁰ = 3×100 + 4×10 + 5×1 = 300 + 40 + 5 = 345 // Binary example: 1011 1011₂ = 1×2³ + 0×2² + 1×2¹ + 1×2⁰ = 1×8 + 0×4 + 1×2 + 1×1 = 8 + 0 + 2 + 1 = 11₁₀ // Hexadecimal example: 2F 2F₁₆ = 2×16¹ + F×16⁰ = 2×16 + 15×1 = 32 + 15 = 47₁₀

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:

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:

Fun Fact

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
00000Start
10001+1
20010Carry
30011+1
40100Carry
50101+1
60110Carry
70111+1
81000Carry
91001+1
101010Carry
151111Max 4-bit
16100005 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.

— Adapted from Leopold Kronecker

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
000000
110001
220010
330011
440100
550101
660110
770111
881000
991001
A101010
B111011
C121100
D131101
E141110
F151111

Notation Conventions

Hexadecimal numbers can be written in several ways to distinguish them from decimal:

Case Insensitivity

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:

// A 32-bit number in binary: 11111111 10101010 01010101 00001111 // Same number in hexadecimal: 0xFFAA550F // Much more readable!

The Magic of 4:1 Ratio

Since 16 = 2⁴, each hexadecimal digit corresponds to exactly 4 binary bits. This creates a perfect mapping:

Key Advantage

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

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.

Example: Convert 110101110101₂ to Hex
Step 1: Group into 4-bit chunks from right:
1101 0111 0101
Step 2: Convert each group:
1101 = D
0111 = 7
0101 = 5
Result: 110101110101₂ = D75₁₆

Hexadecimal to Binary

Method: Convert each hex digit to its 4-bit binary equivalent.

Example: Convert 3F2A₁₆ to Binary
Step 1: Convert each hex digit:
3 = 0011
F = 1111
2 = 0010
A = 1010
Step 2: Combine the binary groups:
0011 1111 0010 1010
Result: 3F2A₁₆ = 0011111100101010₂
Important Note

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.

Example: Convert 10110101₂ to Decimal
Step 1: Write positional values:
1×2⁷ + 0×2⁶ + 1×2⁵ + 1×2⁴ + 0×2³ + 1×2² + 0×2¹ + 1×2⁰
Step 2: Calculate each term:
128 + 0 + 32 + 16 + 0 + 4 + 0 + 1
Result: 10110101₂ = 181₁₀

Decimal to Binary

Method: Repeatedly divide by 2 and record remainders (read bottom to top).

Example: 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
Result: 181₁₀ = 10110101₂ (read remainders from bottom to top)

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.

Example: Convert 2AF₁₆ to Decimal
Step 1: Write positional values:
2×16² + A×16¹ + F×16⁰
= 2×256 + 10×16 + 15×1
Step 2: Calculate:
512 + 160 + 15
Result: 2AF₁₆ = 687₁₀

Decimal to Hexadecimal

Method: Repeatedly divide by 16 and record remainders.

Example: Convert 687₁₀ to Hex
687 ÷ 16 = 42 remainder 15 (F)
42 ÷ 16 = 2 remainder 10 (A)
2 ÷ 16 = 0 remainder 2
Result: 687₁₀ = 2AF₁₆

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:

Example: Convert 4095₁₀ to Binary via Hex
Step 1: Convert to hex:
4095 ÷ 16 = 255 remainder 15 (F)
255 ÷ 16 = 15 remainder 15 (F)
15 ÷ 16 = 0 remainder 15 (F)
So 4095₁₀ = FFF₁₆
Step 2: Convert hex to binary:
F = 1111
F = 1111
F = 1111
Result: 4095₁₀ = 111111111111₂
Pro Tip

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:

/* CSS Color Codes */ color: #FF5733; /* Orange */ background: #000000; /* Black */ border: #FFFFFF; /* White */ /* RGBA with alpha */ color: rgba(255, 87, 51, 0.8);

2. Memory Addresses

Computer memory addresses are typically shown in hexadecimal:

// Memory address example int* ptr = 0x7FFF5FBFF8C0; // Stack pointer in assembly mov rsp, 0x7FFFFFFFE3A0

3. Character Encoding

ASCII and Unicode use hexadecimal values:

Character ASCII (Hex) ASCII (Decimal) ASCII (Binary)
'A'0x416501000001
'a'0x619701100001
'0'0x304800110000
' '0x203200100000
'!'0x213300100001

4. Network Addresses

MAC addresses use hexadecimal notation:

// MAC address format 00:1A:2B:3C:4D:5E // IPv6 address (hexadecimal) 2001:0db8:85a3:0000:0000:8a2e:0370:7334

5. Error Codes

Many systems use hexadecimal error codes:

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

Convert IP 192.168.1.1 to Binary and Hex
Step 1: Convert each octet to binary:
192 = 11000000
168 = 10101000
1 = 00000001
1 = 00000001
Step 2: Binary IP:
11000000.10101000.00000001.00000001
Step 3: Convert to hex:
11000000 = C0
10101000 = A8
00000001 = 01
00000001 = 01
Result: 192.168.1.1 = C0.A8.01.01 in hex

Example 2: Color Code Conversion

Convert #4A90E2 to RGB Values
Step 1: Split into RGB components:
Red: 4A
Green: 90
Blue: E2
Step 2: Convert each to decimal:
4A₁₆ = 4×16 + 10 = 74
90₁₆ = 9×16 + 0 = 144
E2₁₆ = 14×16 + 2 = 226
Result: #4A90E2 = RGB(74, 144, 226)

Example 3: Large Number Conversion

Convert 1,000,000₁₀ to Binary and Hex
Method: Convert to hex first (easier):
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)
Hex result: F4240₁₆
Convert to binary:
F = 1111
4 = 0100
2 = 0010
4 = 0100
0 = 0000
Result: 1,000,000₁₀ = F4240₁₆ = 11110100001001000000₂
Verification

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

Practice Problems

Try these conversion problems to test your skills:

  1. Convert 10110110₂ to hexadecimal
  2. Convert 0xDEADBEEF₁₆ to binary
  3. Convert 255₁₀ to binary and hex
  4. Convert 0xFF₁₆ to decimal
  5. Convert 1111111111111111₂ to hex and decimal
Answer Key

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

Next Steps

  1. Practice conversions - Work through examples daily
  2. Use calculators - Verify your manual calculations
  3. Explore programming - See these concepts in action
  4. Study computer architecture - Understand why binary matters
  5. 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.

— Computer Science Wisdom

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.