Chapter 25: Hexadecimal Numbers

Part 1: What Are Hexadecimal Numbers?

Hexadecimal (often called “hex”) is a base-16 number system. Unlike our familiar decimal system (base-10) which uses ten digits (0-9), hexadecimal uses sixteen symbols:

  • Digits 0-9 represent values 0 through 9

  • Letters A-F represent values 10 through 15

Hexadecimal Digit Decimal Value Binary (4 bits)
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

Part 2: Why Use Hexadecimal?

1. Compact Representation

One hexadecimal digit represents exactly 4 binary digits (a “nibble”). This means a byte (8 bits) can be represented by just two hexadecimal digits.

For example, the binary number 11110101 becomes F5 in hex—much shorter and easier to remember!

2. Easy Conversion to/from Binary

Converting between hex and binary is straightforward because each hex digit corresponds directly to a 4-bit binary group. There’s no complex calculation—just memorize the table above or learn the pattern.

3. Common in Programming

Hexadecimal appears everywhere in programming:

  • Memory addresses (when debugging, you see addresses like 0x7FFF

  • Color codes in web design (#FFA500 for orange)

  • Character encodings (ASCII/Unicode values)

  • Machine code and assembly language

  • Network protocols (MAC addresses, IPv6 addresses)

Part 3: Hexadecimal Place Values

Just like decimal (powers of 10) and binary (powers of 2), hexadecimal uses powers of 16.

The rightmost digit is the 16⁰ place (1’s place)
The next digit is the 16¹ place (16’s place)
Then 16² (256’s place)
Then 16³ (4096’s place)
And so on…

Example: The hex number 2A3F means:

Digit 2 A 3 F
Place Value 16³ = 4096 16² = 256 16¹ = 16 16⁰ = 1
Contribution 2 × 4096 = 8192 10 × 256 = 2560 3 × 16 = 48 15 × 1 = 15

Total = 8192 + 2560 + 48 + 15 = 10,815 in decimal

Part 4: Converting Between Number Systems

Converting Hexadecimal to Decimal

To convert hex to decimal, multiply each digit by its place value and add them up.

Example 1: Convert 1A to decimal.

Digit 1 A
Place Value 16¹ = 16 16⁰ = 1
Contribution 1 × 16 = 16 10 × 1 = 10

Total = 16 + 10 = 26 decimal

Example 2: Convert FF to decimal.

Digit F F
Place Value 16¹ = 16 16⁰ = 1
Contribution 15 × 16 = 240 15 × 1 = 15

Total = 240 + 15 = 255 decimal (which is why 0xFF is a common value for maximum in many contexts)

In Python:

python

Converting Decimal to Hexadecimal

To convert decimal to hex, repeatedly divide by 16 and collect the remainders from bottom to top.

Example: Convert decimal 255 to hexadecimal.

Division Quotient Remainder Hex Digit
255 ÷ 16 15 15 F
15 ÷ 16 0 15 F

Reading remainders from bottom to top: FF

Example: Convert decimal 10815 to hexadecimal.

Division Quotient Remainder Hex Digit
10815 ÷ 16 675 15 F
675 ÷ 16 42 3 3
42 ÷ 16 2 10 A
2 ÷ 16 0 2 2

Reading remainders from bottom to top: 2A3F

In Python:

python

Converting Between Hexadecimal and Binary

This is where hex really shines! Each hex digit corresponds to exactly 4 binary digits.

Example: Convert 2A3F to binary.

Break it down digit by digit:

  • 2 = 0010

  • A = 1010

  • 3 = 0011

  • F = 1111

Combine: 0010 1010 0011 1111 or 0010101000111111 (16 bits)

Example: Convert binary 110110101001 to hex.

First, group binary into 4-bit chunks from the right, padding with zeros on the left if needed:

  • 1101 1010 1001 (already in groups of 4)

  • 1101 = D

  • 1010 = A

  • 1001 = 9

Result: DA9

In Python:

python

Part 5: Hexadecimal in Programming Languages

Literals and Notation

Different programming languages use different prefixes for hexadecimal literals:

python

javascript

java

c

r

Common Uses

1. Memory Addresses

When debugging, you’ll often see memory addresses in hex:

python

2. Color Codes in Web Development

Colors in HTML/CSS are often specified in hex:

css

Each pair of hex digits represents the intensity of Red, Green, and Blue:

  • #FF0000 = Red (255, 0, 0)

  • #00FF00 = Green (0, 255, 0)

  • #0000FF = Blue (0, 0, 255)

  • #FFFFFF = White (255, 255, 255)

  • #000000 = Black (0, 0, 0)

python

3. Character Codes

ASCII and Unicode values are often shown in hex:

python

Output:

text

4. File Signatures (Magic Numbers)

File formats often start with specific hex sequences called “magic numbers” that identify the file type:

python

5. Bit Manipulation

Hexadecimal is often used in bit manipulation because each digit represents exactly 4 bits:

python

Part 6: Hexadecimal Arithmetic

Addition

Adding hex numbers is like decimal addition, but you carry when you reach 16 instead of 10.

Example: Add A5 + 3F

text

  1. Rightmost column: 5 + F (15) = 20 decimal. 20 in hex is 14 (since 16 + 4). Write 4, carry 1.

  2. Next column: 1 (carry) + A (10) + 3 = 14 decimal. 14 in hex is E.

Result: E4

python

Subtraction

Similar to decimal subtraction, borrowing when needed.

Example: Subtract 3F from E4

text

  1. Rightmost column: 4 – F (15) → need to borrow. Borrow 1 from the left, making the right column 14 (hex). 14 – 15? Wait, that’s still negative. Let’s do it carefully.

Better to convert to decimal: E4 = 228, 3F = 63, 228 – 63 = 165 = A5 in hex.

Part 7: Common Hexadecimal Values to Know

Some hex values you’ll encounter frequently:

Hex Decimal Binary Common Use
0x0 0 0000 Zero, false
0x1 1 0001 One, true
0xF 15 1111 Maximum for 4 bits
0xFF 255 11111111 Maximum byte value
0xFFFF 65535 16 bits of 1s Maximum 16-bit value
0x7F 127 01111111 Maximum positive signed byte
0x80 128 10000000 Minimum negative signed byte (in two’s complement)
0xA 10 1010 Decimal 10
0x10 16 00010000 One 16 (like decimal’s 10)

Part 8: Practical Exercises

Exercise 1: Color Converter

python

Exercise 2: Memory Dump Viewer

python

Exercise 3: Bitmask Viewer

python

Summary: The Hexadecimal Philosophy

  • Hexadecimal is a base-16 number system using digits 0-9 and letters A-F.

  • One hex digit represents exactly 4 bits (a nibble), making it a perfect shorthand for binary.

  • Conversion between hex and binary is trivial—just translate each hex digit to/from 4 bits.

  • Common uses include memory addresses, color codes, character encoding, file signatures, and bit manipulation.

  • In most programming languages, hex literals are written with a 0x prefix (e.g., 0xFF).

  • Hexadecimal arithmetic works like decimal but carries at 16 instead of 10.

Hexadecimal is one of the programmer’s most trusted tools. It sits perfectly between the binary world of machines and the decimal world of humans, making it indispensable for systems programming, debugging, and any work that requires peeking under the hood of your computer.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *