Hexadecimal, often shortened to “hex,” is a base-16 number system that plays a crucial role in the world of computers and digital electronics. Like the familiar decimal system (base-10) and the binary system (base-2), hexadecimal provides a way to represent numerical values, but it does so with 16 distinct symbols. This system is particularly useful in computing because it offers a more human-friendly way to represent binary code, which is the language of computers.
In hexadecimal, we use the digits 0-9, just as in decimal. To represent values beyond nine, hex borrows letters from the alphabet: A, B, C, D, E, and F. These letters correspond to the decimal values 10, 11, 12, 13, 14, and 15, respectively. Each position in a hexadecimal number represents a power of 16, making it a compact way to express large numbers. A key feature of hexadecimal is its direct relationship with binary: each hexadecimal digit neatly corresponds to exactly four binary digits (bits), also known as a nibble. This makes conversion between binary and hexadecimal exceptionally straightforward and efficient. For instance, the binary number 1010101010 can be represented as 2AA in hexadecimal, significantly shortening the representation and making it easier to read and manipulate. This compression is invaluable for computer systems dealing with vast amounts of binary data.
Here’s a table summarizing the relationships between hexadecimal, binary, and decimal values for quick reference:
Hex | Binary | Decimal |
---|---|---|
0 | 0 | 0 |
1 | 1 | 1 |
2 | 10 | 2 |
3 | 11 | 3 |
4 | 100 | 4 |
5 | 101 | 5 |
6 | 110 | 6 |
7 | 111 | 7 |
8 | 1000 | 8 |
9 | 1001 | 9 |
A | 1010 | 10 |
B | 1011 | 11 |
C | 1100 | 12 |
D | 1101 | 13 |
E | 1110 | 14 |
F | 1111 | 15 |
14 | 10100 | 20 |
3F | 111111 | 63 |
Hexadecimal to Decimal Conversion
Converting from hexadecimal to decimal involves understanding the positional value system. In hexadecimal, each digit’s place value is a power of 16. Starting from the rightmost digit, the place values are 160 (ones place), 161 (sixteens place), 162 (256s place), and so on. To convert a hex number to decimal, you multiply each hex digit by its corresponding place value and sum the results. Remember that the hex letters A through F represent the decimal values 10 through 15.
Let’s take the hexadecimal number 2AA as an example:
EX: | 2AA = (2 × 162) + (A × 161) + (A × 160) |
---|---|
= (2 × 256) + (10 × 16) + (10 × 1) | |
= 512 + 160 + 10 = 682 |
Thus, the hexadecimal value 2AA is equal to 682 in decimal.
Decimal to Hexadecimal Conversion
Converting from decimal to hexadecimal is a slightly more involved process but relies on the same principles of place values. The method involves repeatedly dividing the decimal number by 16 and noting the remainders. These remainders, read in reverse order, form the hexadecimal number.
Here are the steps to convert a decimal number to hexadecimal:
- Find the largest power of 16 that is less than or equal to the decimal number you want to convert (let’s call this number X).
- Determine how many times this power of 16 goes into X. This number will be the hex digit for that place value.
- Multiply the digit found in step 2 by the power of 16 and subtract this value from X. The result is a new value (let’s call it Y).
- The digit from step 2 is the hexadecimal digit for the place value corresponding to the power of 16 found in step 1.
- Repeat steps 1-3 using Y as the new starting value. Continue this process until the remaining value is less than 16. The final remainder will be the least significant digit (rightmost).
- Assign each digit found in step 2 to its respective place value to construct the hexadecimal number. Remember to convert decimal remainders 10-15 to their hexadecimal letter equivalents (A-F).
Let’s convert the decimal number 1500 to hexadecimal:
EX: | Convert decimal 1500 to hex |
---|---|
(1) Largest power of 16 less than 1500 is 162 = 256 | |
(2) 1500 ÷ 256 = 5 with a remainder. So, 256 goes into 1500 5 times. (5 × 162) | |
(3) 1500 – (5 × 256) = 1500 – 1280 = 220. New value Y = 220 | |
(4) Largest power of 16 less than 220 is 161 = 16. 220 ÷ 16 = 13 with a remainder. So, 16 goes into 220 13 times. (13 × 161) | |
(5) 220 – (13 × 16) = 220 – 208 = 12. New value is 12. | |
(6) 16 is larger than 12, so 12 is the value for the 160 place value. (12 × 160) | |
(7) 1500 = (5 × 162) + (13 × 161) + (12 × 160) | |
(8) Convert decimal digits 13 and 12 to hex letters: 13 = D, and 12 = C | |
(9) Therefore, the hexadecimal value of 1500 is: 5DC |
Converting back from hex to decimal is a good way to check your conversion accuracy.
Hexadecimal Arithmetic
Just like decimal and binary numbers, you can perform arithmetic operations such as addition, subtraction, multiplication, and division with hexadecimal numbers.
Hexadecimal Addition
Hexadecimal addition follows the same principles as decimal addition, but you need to remember that you are working in base-16. When the sum of digits in a column exceeds 15, you carry over to the next column. It’s helpful to recall the decimal equivalents of the hex letters A-F if you are not yet familiar with hex arithmetic.
Here’s an example of hexadecimal addition:
EX: | | | 1 | 8 | 1A | B |
|—|—|—|—|—|—|
| + | | | | B | 7 | 8 |
| = | | | 1 | 4 | 2 | 3 |
In the rightmost column, B + 8 (hex) is equivalent to 11 + 8 = 19 (decimal). In hex, 19 is represented as 13 (16 + 3). So, we write down 3 and carry over 1 to the next column. In the next column, 1 (carry-over) + A + 7 (hex) is 1 + 10 + 7 = 18 (decimal). In hex, 18 is 12 (16 + 2). Write down 2 and carry over 1. In the next column, 1 (carry-over) + 8 + B (hex) is 1 + 8 + 11 = 20 (decimal), which is 14 in hex. Write down 4 and carry over 1. Finally, write down the carry-over 1 in the leftmost column. The result is 1423 in hexadecimal.
Hexadecimal Subtraction
Hexadecimal subtraction is similar to decimal subtraction, including the concept of borrowing. The key difference is that when you borrow in hexadecimal, you are borrowing 16 (decimal) instead of 10 (decimal).
Consider this example of hexadecimal subtraction:
EX: | | | 5 | D | 1C |
|—|—|—|—|—|
| – | | | 3 | A | F |
| = | | | 2 | 2 | D |
Starting from the rightmost column, C (12 decimal) minus F (15 decimal) requires borrowing. Borrow 1 from the next column (D becomes C), which adds 16 (decimal) to C. So, we have (16 + 12) – 15 = 13 (decimal), which is D in hex. In the next column, now we have C – A (hex), which is 12 – 10 = 2 (decimal). In the leftmost column, 5 – 3 = 2 (decimal). The result is 22D in hexadecimal.
Hexadecimal Multiplication
Hexadecimal multiplication can be more complex, especially for larger numbers. It often involves converting to decimal, performing multiplication, and converting back, or using a hexadecimal multiplication table.
Here is an example of hexadecimal multiplication:
EX: | | | | F | A |
|—|—|—|—|—|
| × | | | | C | 3 |
| | | | 2 | E | E |
| + | | B | B | 8 | 0 |
| = | | B | E | 6 | E |
To perform this multiplication:
- Multiply 3 by A (hex). 3 × 10 (decimal) = 30 (decimal) = 1E (hex). Write down E, carry over 1.
- Multiply 3 by F (hex) and add the carry-over. (3 × 15) + 1 = 46 (decimal) = 2E (hex). Write down 2E. First partial product is 2EE.
- Multiply C by A (hex). 12 × 10 = 120 (decimal) = 78 (hex). Write down 8, carry over 7.
- Multiply C by F (hex) and add the carry-over. (12 × 15) + 7 = 187 (decimal) = BB (hex). Write down BB. Second partial product is BB8, shifted one position to the left.
- Add the partial products 2EE and BB80 using hexadecimal addition (as explained earlier) to get BE6E.
Hexadecimal Division
Hexadecimal division, particularly long division, mirrors decimal long division but requires hexadecimal multiplication and subtraction at each step. Alternatively, one can convert to decimal, divide, and convert back.
Here’s an example of hexadecimal long division:
As with multiplication, a hexadecimal multiplication table can be very useful for hexadecimal division.
Conclusion
Understanding hexadecimal is essential for anyone working with computers, programming, or digital systems. Its compact representation of binary data and straightforward conversion make it a valuable tool. Whether you’re dealing with memory addresses, color codes, or data representation, hexadecimal provides a bridge between human readability and the binary world of machines. Mastering hexadecimal conversion and arithmetic operations will significantly enhance your technical toolkit.