2.6 - Hexadecimal Numbers
What is hexadecimal?
Hexadecimal, often shortened to hex, is a base-16 number system commonly used in computer science and programming. Unlike the denary (base-10) system, which uses digits 0-9, hexadecimal uses 16 symbols: the digits 0-9 and the letters A-F to represent values from 10 to 15.
Key features of hexadecimal:
- Each hexadecimal digit corresponds to a value between 0 and 15 in denary.
- The letters A to F represent denary values 10 to 15: A=10, B=11, C=12, D=13, E=14, F=15.
- Hexadecimal is closely linked to binary (base-2) because one hex digit exactly represents four binary digits, known as a nibble.
Reasons for using hexadecimal in programming
Programmers frequently use hexadecimal because it offers several advantages over binary, especially when dealing with large numbers or low-level programming tasks. Although computers ultimately convert everything to binary for processing, hexadecimal provides a more manageable way to represent and work with binary data.
Advantages of hexadecimal over binary:
- Shorter representation - Hexadecimal numbers are much shorter than their binary equivalents, making them easier to read and remember.
- Reduced errors - With fewer characters, there is less chance of mistakes when entering or copying numbers.
- Easier conversions - Converting between binary and hexadecimal is straightforward, as each hex digit matches exactly one nibble, which is simpler than converting between binary and denary.
Converting hexadecimal to denary
To convert a hexadecimal number to denary, you use place values that are powers of 16, starting from the rightmost digit (160 = 1) and increasing to the left (161 = 16, 162 = 256, and so on). Multiply each hex digit by its place value and add the results.
Steps for hexadecimal to denary conversion:
- Write the hexadecimal number and identify each digit's place value, starting from the right.
- Convert any letter digits (A-F) to their denary equivalents (10-15).
- Multiply each digit by its place value (power of 16).
- Add all the results to get the denary number.
Worked example - Converting hexadecimal to denary
Convert the hexadecimal number 9D to denary.
Step 1: Identify the place values and digits
- Place values: 16 (for 9) and 1 (for D).
- Digits: 9 (denary 9) and D (denary 13).
Step 2: Multiply each digit by its place value
- 9 × 16 = 144
- 13 × 1 = 13
Step 3: Add the results
144 + 13 = 157
So, 9D in hexadecimal is 157 in denary.
Converting denary to hexadecimal
Converting from denary to hexadecimal involves dividing the denary number by powers of 16 and finding the remainders, which become the hex digits (converting remainders 10-15 to A-F).
Steps for denary to hexadecimal conversion:
- Find the highest power of 16 that is less than or equal to the denary number.
- Divide the denary number by that power of 16 to get the digit for that place.
- Take the remainder and repeat with the next lower power of 16.
- Continue until you reach the units place (160 = 1).
- Convert any digits from 10-15 to A-F.
Worked example - Converting denary to hexadecimal
Convert the denary number 173 to hexadecimal.
Step 1: Divide by the highest power of 16
- Highest power: 16 (256 is too big).
- 173 ÷ 16 = 10 remainder 13 (10 is A, 13 is D).
Step 2: Confirm the digits
- Digit for 16: A (10)
- Digit for 1: D (13)
Step 3: Combine the digits
AD
So, 173 in denary is AD in hexadecimal.
Converting binary to hexadecimal
Converting binary to hexadecimal is efficient because each group of four binary digits (a nibble) directly corresponds to one hex digit. This makes it simpler than binary-to-denary conversions.
Steps for binary to hexadecimal conversion:
- If the binary number's length is not a multiple of 4, add leading zeros to make complete nibbles.
- Split the binary number into groups of four bits, starting from the right.
- For each nibble, calculate its denary value by adding the place values (8, 4, 2, 1) where there is a 1.
- Convert the denary value to the corresponding hex digit (0-9 or A-F).
- Combine the hex digits.
Worked example - Converting binary to hexadecimal
Convert the binary number 11010110 to hexadecimal.
Step 1: Split into nibbles
1101 0110 (already even, no padding needed).
Step 2: Convert first nibble (1101)
- 8 (1) + 4 (1) + 2 (0) + 1 (1) = 8 + 4 + 1 = 13 = D
Step 3: Convert second nibble (0110)
- 8 (0) + 4 (1) + 2 (1) + 1 (0) = 4 + 2 = 6
Step 4: Combine the hex digits
D6
So, 11010110 in binary is D6 in hexadecimal.
Worked example - Converting binary to hexadecimal with padding
Convert the binary number 101011 to hexadecimal.
Step 1: Add leading zeros for complete nibbles
0010 1011 (padded to 8 bits).
Step 2: Convert first nibble (0010)
- 8 (0) + 4 (0) + 2 (1) + 1 (0) = 2
Step 3: Convert second nibble (1011)
- 8 (1) + 4 (0) + 2 (1) + 1 (1) = 8 + 2 + 1 = 11 = B
Step 4: Combine the hex digits
2B
So, 101011 in binary is 2B in hexadecimal.
Converting hexadecimal to binary
To convert hexadecimal to binary, reverse the process: convert each hex digit to its four-bit binary equivalent and combine them.
Steps for hexadecimal to binary conversion:
- For each hex digit, find its denary value (0-15).
- Convert that denary value to a four-bit binary number using place values 8, 4, 2, 1.
- Write the binary nibbles together to form the full binary number.
- Leading zeros in nibbles can be included for clarity but are not always necessary in the final binary representation.
Worked example - Converting hexadecimal to binary
Convert the hexadecimal number C7 to binary.
Step 1: Convert first digit (C)
- C = 12 in denary
- 12 in binary: 8 (1) + 4 (1) + 2 (0) + 1 (0) = 1100
Step 2: Convert second digit (7)
- 7 = 7 in denary
- 7 in binary: 8 (0) + 4 (1) + 2 (1) + 1 (1) = 0111
Step 3: Combine the binary nibbles
11000111
So, C7 in hexadecimal is 11000111 in binary.