Wednesday, July 14, 2010

Reading Hexadecimal Numbers and Understanding Binary Numbers

This post is aimed towards beginners trying to understand hexadecimal numbers, and why programmers often use them instead of base-10 decimal.

Well computers internally deal with logic circuits and we have a concept of a 'bit', which represents either '1' or '0', 'on' or 'off', etc...

You can put 8 bits together and form a byte. These 8 bits can then be interpreted as a binary string to represent a number.
The first bit, called the least-significant bit, represents 2^0 power (0 or 1).
The second bit, represents 2^1 power (0 or 2)
The third bit, represents 2^2 power (0 or 4), etc...

So in one byte, you have 8-bits that represent this:

b7, b6, b5, b4, b3, b2, b1, b0
2^7, 2^6, 2^5, 2^4, 2^3, 2^2, 2^1, 2^0


All these bit-values are then added together to give you the value of the number.

For example, '4' is represented as "00000100", notice that b2 is set (2^2)
Likewise, '5' is "00000101", notice that b2 and b0 are set (2^2 + 2^0 == 4 + 1 == 5)
And so on...


Now because internally numbers are represented in this format, you should understand that binary numbers are pretty important to programmers, especially low-level programmers.

But notice that it takes 8 digits to represent 1-byte, a byte only represents 1-char in c++.
But usually we deal with ints, these don't just represent 1-byte, but instead 4-bytes (usually).

That means it will take 8*4 = 32 digits to represent an integer in binary!
You can't expect a programmer to be writing 32-digit numbers full of 1's and 0's. There has to be a better way!

That's where hexadecimal comes in.
It just so happens that 1-digit in hexadecimal, represents exactly 4-digits in binary. This is very useful to us, because that means if we memorize the hexadecimal digits, then we can easily refer to 4-binary digits with just 1 hexadecimal digit.

The 16 digits in hexadecimal are:

Hex | Binary
--------------
0 | 0000
1 | 0001
2 | 0010
3 | 0011
4 | 0100
5 | 0101
6 | 0110
7 | 0111
8 | 1000
9 | 1001
a | 1010
b | 1011
c | 1100
d | 1101
e | 1110
f | 1111


The key to using hexadecimal fluently, is memorizing this hex-to-binary table.
I'll give you some hints to remember some of the numbers:
0 is 0 in hex and in binary.
1 is 1 in hex and in binary.
1, 2, 4, and 8 only have 1-bit set (because they're powers of 2)
f is the last hex number, and has all 4-bits set (all 1's)
'a' comes after 9, which in decimal should be '10', if you notice though, 'a' represents '1010' in binary, so think of two 10's.


Anyways, it takes some time to memorize the table, but once you do, you are then able to convert hex numbers to binary very easily.
For example:

7 f
0x7f = | 0111 | 1111 |

5 5
0x55 = | 0101 | 0101 |

a b c d e f 9 8
0xabcdef98 = | 1010 | 1011 | 1100 | 1101 | 1110 | 1111 | 1001 | 1000 |



As you can see by the last example, we were able to represent a 32-bit number with just 8 digits. Instead of typing "10101011110011011110111110011000" we just had to type "0xabcdef98".

Oh and just so you know, c++ uses the prefix "0x" to refer to hexadecimal numbers.
Sometimes people use "$" to refer to hex, or sometimes they just use "x" as the prefix.

Also I should note that the reason you can't use normal base-10 decimal numbers to refer to binary easily, is because a single decimal digit does not equal an exact amount of binary digits. 3-binary digits is octal, and 4-binary digits is hex. Decimal is in-between those, so you can't use decimal to refer to binary numbers nicely.

Anyways I hope this article helps those who were having a bit of trouble understanding hex.

6 comments:

  1. best explanation I've read yet. Nice job!

    ReplyDelete
  2. Glad i end up in this page. It all makes sense now. Thanks!

    ReplyDelete
  3. Very impressive, perhaps you should take up teaching!

    ReplyDelete
  4. Thank you so much, I read a lot of BS and they couldn't explain!
    You explanation is perfect,
    I wish you have had posted also the conversion formula for all of this to each other.
    For example decimal to binary and vise versa

    ReplyDelete
  5. I agree with all those above, thank you!

    ReplyDelete
  6. if you are not a professor, you should probably be one

    ReplyDelete