Friday, October 23, 2020

You DO Know Hex...


...you just don't know it yet. Allow me to explain.

Back in elementary school, teachers taught us about the ones, tens, hundredths, ... columns. So the number 894 has 8 in the hundredths column, 9 in the tens column, and 4 in the ones column.

More specifically...

894 = (8 * 100) + (9 * 10) + (4 * 1)

Do you see a pattern with the ones, tens, and hundredths columns? They are all powers of ten: 100, 101, and 102. This is the numbering system we learned in school: base 10. The valid numbers in a column are 0 - 9. That makes sense, because we can only have a single digit in a column.

Now in the hexadecimal number system, the base is 16. So the first three columns are the ones, sixteens, and two hundred fifty-sixths columns. In powers of sixteen, they are: 160, 161, and 162. So the number 894 in hexadecimal is:

89416 = (8 * 256) + (9 * 16) + (4 * 1) = 219610

Note that the subscript indicates the base. 894 is the hexadecimal number. 2196 is the base 10 equivalent. You can also represent hexadecimal numbers like this: 0x894. For the 2196 base 10 number, we typically omit the 10 subscript since it is assumed.

The hexadecimal story continues a bit more. If base 10 uses the numbers 0 - 9, then what does base 16 use? Base 16 must have 16 possible digits. Its range of digits start with the same digits 0 - 9. The remaining six digits are: A, B, C, D, E, and F. The letters stand for 10 - 15 respectively. So, counting in hexadecimal is like this:

0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F

I guess it would be too confusing in elementary school to combine numbers and letters in math. But it would've been fun trying.

For a final example, here is the conversion of hexadecimal F0A to base 10:

F0A16
= (F * 162) + (0 * 161) + (A * 160)
= (15 * 162) + (0 * 161) + (10 * 160)
= (15 * 256) + (0 * 16) + (10 * 1)
= (3840) + (0) + (10)
= 385010 
So there you have it. Hexadecimal numbers made easy! You already know hexadecimal. Just change the base from 10 to 16. And have a cup of 0xC0FFEE while you're at it!

No comments:

Post a Comment

What is Git Markdown?

Git Markdown is a markup language that creates HTML-like documents for web-based Git repos like GitHub, GitLab, and Bitbucket. The documents...