Binary Hexadecimal and Decimal Number Systems

Written: 2009/07/30

While working with computers you will often come accross three types of number systems. Decimal, that we all use in our every day lives. Binary, the language of computers (ones and zeroes), and Hexadecimal, a more human-friendly representation of binary. This article aims to tie together these 3 number systems in as simple a way possible for people who want to learn about it.

Decimal is the number systems we're all used to. It uses the digits 0 through 9 to represent any number we can imagine. When you count in decimal, you start off with 0 and go up to 9. When you reach 9 and run out of digits, you move one space to the left and start over again. This is the only difference between all these number systems. All that changes is how many symbols you cycle through before starting over and shifting to the left.

Decimal is base10, which means you have 10 symbols to cycle through. Binary is base2, so all you have is a 1 and a 0, and Hexadecimal is to the base16, with 16 symbols. You can pretty much use any symbols you want to represent these, if you managed to invent 10 new symbols to replace 0 - 9, you could count using those instead, however to keep things simple this article makes use of what most sane humans use, 0 - 9, 0 - 1, and 0 - F.

I will label hex numbers with 0x before the number (0xFF08BA), Decimal with either no label or a "d" at the end, and binary with nothing or a "b" at the end, depending on context (if it's obvious i'm using bin or dec there's no reason to label).

Counting in Decimal
I'm quite sure that if you're able to read this, you're also able to count in decimal, However I'll be writing on all three number systems so that if you don't understand binary and hex, you can always read about decimal and find your way.

When counting in any number system, you will start with the lowest value symbol (or digit in this case), and cycle upwards until you reach the end. So: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9. Oops. No more digits. How will you represent the value that comes after 9?
With decimal this is easy because we do this every day. You write "10" after "9" because you know that this is how it is, you learned in school, probably on your first day how to count to 10, but no one tells you how the process of counting actually works. So what exactly happened here? This is the vital part, understand this process and counting in other number systems will be easy regardless of base.

What happened here is you added one more position to your number by starting back at 0, but keeping track of that action by placing a 1 to the left of it. Once you reach 9 again, you will do the same thing, your 9 becomes a 0, and this time the 1 becomes a 2, indicating that this is the second time this event has occured. Think of it as keeping a tally. In decimal, every time you cycle through the 10 symbols, you make note of it and begin the cycle again. Due to living in a world of base10 (decimal), we do this without thinking. But what if someone asked you to count to 10 in base2 (binary). Would you be able to? The average person probably won't even get to 5.

Counting in Binary
Counting in binary is theoretically easier than decimal. You only have to remember a 1, and a 0, and the tally keeping system mentioned earlier.

Let's start with a simple example, counting to 10 in binary.

0    =    0000
1    =    0001
2    =    0010
3    =    0011
4    =    0100
5    =    0101
6    =    0110
7    =    0111
8    =    1000
9    =    1001
10   =    1010

Looks normal for 0 and then 1. Once you get to 2, something you're not used to seeing happens. Remember binary only has 2 symbols to cycle through. So the shifting to the left and starting over happens much more often here than in decimal. This is actually all you need to know for counting in binary. Instead of 0 - 9, it's 0 - 1. Start with 0, go to 1, put a 1 on the left, reset the first 1 to 0, and repeat until you get dizzy from staring at a wall of 1's and 0's. Which is where Hexadecimal comes in.

Counting in Hexadecimal
All hexadecimal really is for is to stop people from getting dizzy and vomiting on their expensive computers, or from going insane and killing everyone in their office with a stapler and letter opener. As you might have noticed, binary can quickly turn into a messy wall of zeroes and ones that litterally can make you dizzy from staring at it (try writing out 0d - 100d in binary form using 8 digits (00000000, 00000001, 00000010, ...).

Our good friend Hexadecimal comes in to save the day by making binary look much better to our eyes. All binary can easily be switched over to hexadecimal by simply looking at a chart, which I will show you very soon. First, we count in hex.

Since hexadecimal is base 16, and we only have 10 digits (0 - 9), a few letters were kidnaped from the alphabet to fill in for the 11th to 16th places. The letters are A through F, A being the lower value (11th place with decimal value of 10) and F the highest (16th place, with decimal value of 15).

Now that you know that important bit of information (assuming you never heard of hexadecimal before), you should be able to count up to 15 in hex now.

0    =    0
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

So that was easy enough. What if you were to go past 15d, to say 26d? 26d would be 0x1A.

Converting Decimal, Binary and Hexadecimal.
What if you had to convert larger numbers from one of these number systems into another? Just knowing how to count isn't going to help you at all unless you'd like to spend all day counting up those numbers. You can obviously use your calculator, but what if all you have is pen and paper? First, if you have trouble remembering these, making the chart helps:

This is easy to make. All you have to know is how to count from 0 to 15 in the three number systems and do so in 3 columns.

Now lets start off with the easiest part. Converting Hex to Binary. Let's say you have this binary number:
01001110

Converting it into hex is easy. All you have to know is your bits and bytes. Each hexadecimal digit is representing 4 bits. That 8 digit binary number is 1 byte. 1 byte is 8 bits. So break the number into 2 pieces:
0100, 1110

Now reference your trusty chart and find out that 0100b is 0x4 and 1110b is 0xE. Meaning that 01001110b is 0x4E.

What if someone is evil and gives you a massive binary number you ask? Not a problem:
0010110101100100011010010

The first thing you should notice is the length of the binary string. It's 25 bits long (or if it's on a piece of paper you can say it's 25 digits or 25 characters long, doesn't matter). What does this mean? Breaking it up into groups of 4 will leave you with 1 bit all by his lonesome.... wrong. Starting from the right side, break it up into groups of 4. You will then reach that last 0 on the left thats's all alone. Add 3 0's in front to make it easier to look at. This doesn't change the value. You would do the same for a 1 all by itself. You add the amount of zeroes needed to make it a group of 4. Of course if you get to a point where you don't need to add the zeroes anymore because you've mastered the subject, then you don't need to. It's just a way to make it easier to see what you're doing. So that binary string is broken down:
0 0101 1010 1100 1000 1101 0010 0000 0101 1010 1100 1000 1101 0010

Look up your chart:
0x0, 0x5, 0xA, 0xC, 0x8, 0xD, 0x2

Stick it all together and get: 0x05AC8D2

That was the easiest part. Now the slightly more complicated part, Decimal to Binary. Remember that binary is base2. So converting into decimal is quite easy if you remember what you learn in grade school about the positions of numbers (units, tens, hundreds, thousands...). Instead of tens and hundreds, we label those by a number in decimal. So for example this binary string:
10110101

The first 1 on the right is the 0's, the 0 following is the 1's, the second 1 is the 2's, and so on until you get to the 7's on the last 1 (note we started from 0, so the 8th bit is the 7's and not 8's).
Now what do we do with this to convert it into decimal? We use the magical powers of multiplication.

Let's do this bit by bit (litterally), starting from the right:
The first bit is a 1, so 1x2^0 = 1.
Second bit is a 0, so 0x2^1 = 0, ignore.
Third bit is a 1, so 1x2^2 = 4.
Fourth bit is a 0, ignore (result will always be 0 if a bit is 0).
Fifth bit is a 1, 1x2^4 = 16.
Sixth bit is a 1, 1x2^5 = 32.
Sevent bit is a 0, ignore.
And the last bit is a 1, 1x2^7 = 128.

1 + 4 + 16 + 32 + 128 = 181d.

So as you can see, what you do is multiply the base (2 in this case) by the decimal position value. If the binary bit is a 1, you keep it, if the binary bit is a 0, you ignore it (since the result would be 0, and 0 added to anything has no effect), and then add up all the numbers to get your decimal value.

Going the other way is easier. From Decimal to binary, all you have to do is remember those values. It also helps to know that an 8 bit binary string can represent to a max of 255d (that is when all 8 bits are 1's).

To Convert a decimal number into binary, you just see how many of what number fits into a binary string. To keep things simple we'll go with the number 255d converted to binary.
Remember the 8th bit is worth 128d, do we have a 128 in 255? yes.
7th bit is worth 64d, after removing 128 from 255 (127) do we have a 64? yes.
6th bit is worth 32d, do we have a 32 in 63 (127 - 64)? yes.
5th bit is worth 16d, do we have a 16 in 31? yes.
4th bit is worth 8d, do we have an 8 in 15? yes.
3rd bit is worth 4d, do we have a 4 in 7? yes.
2nd bit is worth 2d, do we have a 2 in 3? yes.
1st bit is worth 1d, do we have a 1 in 1? yes.

Now for each bit you answered yes to, that bit is a 1. If you answered no to any, you would make that bit a 0 and move on to the next bit and ask the question. So for 128d, you would only answer yes to the 8th bit, making 10000000b. for 16d you would only answer yes to the 5th bit, making 00010000b.

For numbers larger than 255, you would add on more bits as needed, doubling the decimal value of the bit each time. so a 9th bit would be worth 256d, and 10th bit would be worth 512d and so on.

Converting from decimal to hex is simple, but messy, since we deal with multiples of 16 instead of 2. If you're bad at doing math in your head like I am, this gets annoying. Actually what I do if the numbers get too large is convert dec/hex to binary, then convert the binary to what I need. With the easier number system (multiples of 2) there's less room for error. The human mind seems to be naturally good with 2's.


Similar to binary, except much more messy as I said. Since this is a base16 number system, each hex digit can be worth up to 15 times it's value. For example that 0x3 in 0x10A3E is worth 16^1 (16d) 3 times, or 48. So what we do here is the following, from left to right:
0x1 = 1(16^4) = 65536d
0x0 = 0 ignore
0xA = 10(16^2) = 2560d
0x3 = 3(16^1) = 48d
0xE = 14(1) = 14d
65536d + 2560d + 48d + 14d = 68158d

As you can see, this can become quite messy and troublesome. All that's left is hex to decimal. For this we can use the question method again. Keep in mind the values from the above chart. And lets work backwards using the previous example.
68158d to hex would be done like this:
How many times 65536 fits into it? 1.
After removing that many 65536's how many 4096's fits into 2622? 0.
After removing that many 4096's how many 256's fit into 2622? 10.
After removing that many 256's how many 16's fit into 62? 3.
After removing that many 16's how many 1's fit into 14? 14.
Now use the hex/bin/dec chart from the begining of this article to convert the decimal answers to these questions into hex, and you will get:
0x10A3E.

Recent Articles

Subnetting Explained
Learn how to create subnets, how IP addresses work within a network, what network masks do, and the benefits of subnetting....

Disable Autorun to protect From Viruses
Disable autorun on windows to prevent viruses from running and infecting your system....

Release - Easy Command Prompt
A batch file that brings the power of the command prompt to the average user. Ideal for helping the less technologically inclined folks to troubl...

IP Addresses - Theory and Explanation
Learn the basic theory behind IP addresses, what they are for, how it works, and how the internet is structured around them....

Fix Your Latency with the Nagle Algorithm
Learn how to disable the Nagle Algorithm to fix some of your latency in online gaming....

Search on Osayidan.net