Understanding Binary: Hexadecimal

Posted by Jared Kipe on | 0 Comments

Tags: ,

Topically, hexadecimal is extremely useful for programmers to understand, and makes binary in general quite a bit easier to work with and represent both in code and out.

The Hexadecimal System (base 16)

Binary numbers range from 0 to 1, decimal numbers range from 0-9, and hexadecimal numbers range from 0-15.  To keep it relatively easy to read, and more succinct than writing 10, 11, 12... out the numbers over 9 are remapped to the characters A, B, C...

This allows a single "digit" to be represented by a single "character" in writing.

base 10 base 16 base 10 base 16
0 0 8 8
1 1 9 9
2 2 10 A
3 3 11 B
4 4 12 C
5 5 13 D
6 6 14 E
7 7 15 F

It is customary to write hexadecimal numbers as 0x(hexadecimal), e.g. 0xF9, or 0x23A2.


Justification for Hexadecimal

Now that you know what a hexadecimal number is, and how to recognize them... Why would we bother with a completely new numbering scheme?

Remember how ugly it became working with relatively large binary numbers, say over 100 (7 binary digits)?  Remember when I said it was customary to keep long binary numbers clumped up into 4 digit chunks??

That is because every 4 digit binary number corresponds perfectly with one of these new fangled hexadecimal numbers.

base 2 base 16 base 2 base 16
0000 0 1000 8
0001 1 1001 9
0010 2 1010 A
0011 3 1011 B
0100 4 1100 C
0101 5 1101 D
0110 6 1110 E
0111 7 1111 F

This is great because we can take LONGGG strings of binary and quickly convert them into a much easier to read, write, and say form.

Convert 1010 1101 0011 1001b into hexadecimal

This is actually very easy, we first lookup 1010b from our chart:  0xA

1101b = 0xD

0011b = 0x3

1001b = 0x9

Put them all together and you get the answer:  0xAD39


Convert 0x2F into binary

0010 1111b

Easy enough, right?


"I though computers only understood binary?"

That is true.  But humans and computers are much better faster at converting hexadecimal numbers into their binary equivalents.  For this reason hexadecimal is a convenient way of entering constant numbers in code.


Post your comment


No one has commented on this page yet.

RSS feed for comments on this page | RSS feed for all comments