top of page

Recording Memory Addresses With Hexadecimal

Introduction


I'm willing to wager that at some point in your life, you've heard of the term "binary", the base-2 number system, and I'm even more confident that you've worked with the decimal system, base-10:


0 1 2 3 4 5 6 7 8 9

Binary is used because it's memory-efficient and complies best with a computer's hardware. Decimal is used by us humans because we have 10 fingers to count with.


So why, then, do computer scientists use the base-16 number system, hexadecimal?


Base-16


Sometimes, it's useful to get a scope of binary data that we can actually digest. At first glance, a number like this would be extremely foreign.


101110011110110001

Converting this binary value into decimal would also be inconvenient, as it would take quite a while to multiply each binary digit by its place value and then adding that result together:


1 * 2^0 + 0 * 2^1 + 0 * 2^3 ... 1 * 2^17

That's where hexadecimal can come in handy:


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

Because we can't express any character after 9 without a single "slot" or digit, we resort to letters, A being 10 and F being 15.


Base-16 is a power of two, specifically the fourth power of two, meaning that you can express any group of four binary digits as a single hexadecimal value:


0000 : 0		0001 : 1 ...	1111 : F

This means that every binary number can be cut down to a fourth of its size, making it much easier to read and use as a programmer.


So grouped in fours, that long string of binary digits at the beginning can be condensed down to...

Memory Addresses With 0x Notation


But hang on. When someone types 457, how is their fellow computer-scientist to know if they mean the decimal number 457, or the hexadecimal value 457 (which is 1111 in decimal).


By convention, hexadecimal values are prefixed with 0x to differentiate easily between systems. So 457 in hexadecimal would be written as 0x457.


The main purpose of hexadecimal is to represent addresses in memory that each store some data. Now, with this number system, computer scientists can easily access and refer to memory addresses without having to deal with a long slew of 0's and 1's.


Converting Between Number Systems


If you were wondering how I converted between binary and hexadecimal, you'd raise the base of the number system to the power of the place-value (starting from 0), multiply by the number occupying that place-value, and add.


0x124A

To convert this number from base-16 to base-10:


  1. 16^0 * A + 16^1 * 4 + 16^2 * 2 + 16^3 * 1 =

  2. 1 * 11 + 16 * 4 + 256 * 2 + 4096 * 1 =

  3. 11 + 64 + 512 + 4096 =

  4. 4683


Final Thoughts


So I mentioned that we use base-16 because its a power of two, so a question that may have come up is, why not use the base-8 system, the third power of two?


The simple answer is, octal just isn't as concise as hexadecimal.


Thanks for reading!

Comments


bottom of page