What Are Hexadecimal Numbers?
What Are Hexadecimal Numbers?
The hexadecimal number system (often known simply as 'Hex') is a base-16 system of counting used commonly in computing. Whereas our normal decimal system of counting works in base 10, using the ten different digits 0-9, hexadecimal's base-16 means it uses sixteen digits; 0-9 and the letters A-F.
To understand this further, let's look at some examples.
To understand this further, let's look at some examples.
Building Decimal and Hexadecimal Numbers
Both decimal and hexadecimal start in the same way: 0, 1, 2, 3, ... , 9. Once the decimal has reached 9, it has run out of digits, so we start another column on the left with a 1 and reset the original column to zero to get 10. We then keep on counting 11, 12, 13, etc., until we reach 19 and again reset our righthand column and add one to the left to get 20. Each time a column runs out of digits, we reset it to zero and add one to the column to the left, e.g., 99 to 100, 799 to 800, etc.
Hexadecimal, however, has extra digits A - F to continue through before extra columns are needed. We, therefore, start 0, 1, 2, ..., 8, 9, A, B, C, D, E, and F, where the letters A - F are equivalent to the decimal numbers 10 - 15. Once we reach F, we reset the column and start a new column to the left as in decimal. This next number is written 10. We then continue 10, 11, 12, ... ,19, 1A, 1B, ... , 1F, 20 etc. Each time a column reaches F, we reset it and increase the column to its left.
Decimal's ten digits mean that each column is worth 10 times the column to its right, e.g., tens = 10 × units, hundreds = 10 × tens and so on.
In hexadecimal with its sixteen digits, however, each column is worth 16 times the previous. Therefore, our first column counts ones, the second column counts 16s, the third column counts 162s (or 256s), and so on.
Hexadecimal, however, has extra digits A - F to continue through before extra columns are needed. We, therefore, start 0, 1, 2, ..., 8, 9, A, B, C, D, E, and F, where the letters A - F are equivalent to the decimal numbers 10 - 15. Once we reach F, we reset the column and start a new column to the left as in decimal. This next number is written 10. We then continue 10, 11, 12, ... ,19, 1A, 1B, ... , 1F, 20 etc. Each time a column reaches F, we reset it and increase the column to its left.
Decimal's ten digits mean that each column is worth 10 times the column to its right, e.g., tens = 10 × units, hundreds = 10 × tens and so on.
In hexadecimal with its sixteen digits, however, each column is worth 16 times the previous. Therefore, our first column counts ones, the second column counts 16s, the third column counts 162s (or 256s), and so on.
Converting Between Decimal and Hexadecimal
To convert from hexadecimal to decimal relies upon counting in powers of 16. For example, take the hexadecimal number 3B2. As B is the 11th number in hexadecimal, this converts to 3×16^2+11×16+2×1 = 946.
Similarly, the number EA17 = 14×16^3 + 10×16^2 + 1×16 + 7×1 = 59 927.
Converting from decimal to hexadecimal is a little trickier but achievable with some division. First, work out what the highest power of 16 which will go into your number is. E.g., to convert 1782, we see that 1782>16^2 but <16^3, so we will divide by 16^2 to see how many times this fits and what remainder is left.
1782÷16^2 = 6.96 ... or 6 remainder 246, so our hexadecimal number starts with a 6. We then divide the remainder by 16, so 246÷16 = 15.375 or 15 remainder 6. This gives us F as our middle digit (as F = 15) and 6 as our last digit, so 1782 = 6F6.
Similarly, the number EA17 = 14×16^3 + 10×16^2 + 1×16 + 7×1 = 59 927.
Converting from decimal to hexadecimal is a little trickier but achievable with some division. First, work out what the highest power of 16 which will go into your number is. E.g., to convert 1782, we see that 1782>16^2 but <16^3, so we will divide by 16^2 to see how many times this fits and what remainder is left.
1782÷16^2 = 6.96 ... or 6 remainder 246, so our hexadecimal number starts with a 6. We then divide the remainder by 16, so 246÷16 = 15.375 or 15 remainder 6. This gives us F as our middle digit (as F = 15) and 6 as our last digit, so 1782 = 6F6.
The First 18 Numbers in Decimal and Hexadecimal
Decimal |
Hexadecimal |
1 |
1 |
2 |
2 |
3 |
3 |
4 |
4 |
5 |
5 |
6 |
6 |
7 |
7 |
8 |
8 |
9 |
9 |
10 |
A |
11 |
B |
12 |
C |
13 |
D |
14 |
E |
15 |
F |
16 |
10 |
17 |
11 |
18 |
12 |
Some Uses of the Hexadecimal Number System
We've looked at what the hexadecimal system is, but what is its purpose? As mentioned earlier, its main use is in computing. Computers run on a binary number system of 1s and 0s (read my article on binary numbers to find out more about how this works). This is great for a computer where 0s and 1s are matched to switches being off or on but is difficult to write accurately as numbers that are quite small in decimal can have many digits in binary. e.g., 98 = 11000 in binary.
This is where hexadecimal comes in useful. As 16 = 2^4, one hexadecimal digit is equal to four binary digits e.g. 9 = 0101 and F = 1111. We can therefore write binary numbers in the hexadecimal system by grouping our binary digits into groups of four bits, which are known as 'nibbles'.
For example, the binary number 1101 0011 1010 1001 would become D3A9, which is much easier to read and write without fear of making an error.
Another advantage of hexadecimal is that it can express numbers in fewer digits than decimal, with each place representing one out of a possible sixteen rather than ten, hence uses less memory.
This is where hexadecimal comes in useful. As 16 = 2^4, one hexadecimal digit is equal to four binary digits e.g. 9 = 0101 and F = 1111. We can therefore write binary numbers in the hexadecimal system by grouping our binary digits into groups of four bits, which are known as 'nibbles'.
For example, the binary number 1101 0011 1010 1001 would become D3A9, which is much easier to read and write without fear of making an error.
Another advantage of hexadecimal is that it can express numbers in fewer digits than decimal, with each place representing one out of a possible sixteen rather than ten, hence uses less memory.
Using Hexadecimal for Colour Coding
One of the places you are most likely to have seen hexadecimal numbers while using a computer is in picking colours. When choosing colours on a computer, you generally have the choice between several systems such as hexadecimal, RGB, etc. The decimal RGB system gives the amounts of red, green and blue used in your colour out of a total of 255, so for example, pure red = (255, 0, 0), pure green = (0 255, 0) and pure blue = (0, 0, 255). Mixing these gives different colours, such as (255, 255, 0) for yellow.
We can rewrite these colours by converting each number into hexadecimal and writing it as a six digit number preceded by #. For example, black = (0, 0, 0) = #000000 while red = (255, 0, 0) = #FF0000. (209, 136, 210) gives a shade of pink which can be written as #D188D2 as 209 = D1, etc.
We can rewrite these colours by converting each number into hexadecimal and writing it as a six digit number preceded by #. For example, black = (0, 0, 0) = #000000 while red = (255, 0, 0) = #FF0000. (209, 136, 210) gives a shade of pink which can be written as #D188D2 as 209 = D1, etc.
Comments
Have you used hexadecimal before? Does it make sense to you? Don't forget to leave your comments below.