decimal to binary
I’ve been reading a book on the CCNA certification. Part of the exam has to do with being able to manually convert numbers between binary, decimal and hex. I’ve struggled with that concept of being able to do it on paper an easy way but in this particular book, the author spelled out a very simple way to do this that just clicked and will now stick with me for life. Binary numbers are counted in powers of 2 relative to decimal numbers. That is the most confusing thing that I want to say about the subject. It should get easier from here on.
Typically binary numbers are represented in 8-bits or 1-byte. This just means there are 8 digits. These 8-bits can represent any decimal number between 0 and 255. That’s it, any higher numbers and you have to add more bits to represent it. Now for the fun part. Draw a chart on a piece of paper like this:
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
If you look at the chart, you’ll notice a relation to the numbers. Starting with 2, each number is double the number to the right of it. If you can remember this chart, you are over half way there. So now pretend you have a decimal number like 190 that you need to convert to binary. To convert it, you need to find the numbers in the chart, starting from the left, that will add up to 190. For starters, we know that 128 will fit in to 190 so let’s mark that one off with a one.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 |
The next number, 64 won’t fit. That would add up to 192 so we’ll mark that one with a zero.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 0 |
Think of it like playing blackjack. You want to get to 21 but you never want to go over. So keep adding numbers left to right until you reach the correct number, if you go over, skip that number. Adding 32 brings us to 160, add 16 and that gives us 176. We’re still not over so keep going.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 0 | 1 | 1 |
Adding 8 brings us to 184, 4 more gives us 188. 2 more gives us 190 and we’re done so zero out the last bit.
128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 |
So there you have it. 190 in decimal works out to 10111110 in binary. With a little practice, you can easily do these in your head. As you can see from the chart, it’s even easier to go backwards. Just add up the numbers that are set to one and skip the ones that are set to zero. Now for the bonus round!
binary to hexadecimal
Hex is something that a lot of people find confusing. I still find it confusing to convert decimal to hex and vice versa but I have found that it’s VERY easy to convert from binary to hex and back. Let’s take the same number we were just working with. 190 or 10111110. To do hex, we have to split the one byte into two nibbles. This is easier than it sounds. We just split the chart into two halves but write the binary number straight across as if nothing has changed. This is because each hex number can only represent “0” through “15” or more accurately “0” through “F”. The chart looks like this now:
8 | 4 | 2 | 1 | 8 | 4 | 2 | 1 | |
1 | 0 | 1 | 1 | 1 | 1 | 1 | 0 |
So now all you do is add up each nibble separately. The first one on the left gives us 11 or “B”. The second nibble adds up to 14 or “E”. So 190 in decimal equals 10111110 in binary which equals “BE” in hexadecimal. Personally I find it easier to convert decimal to binary before trying to convert it to hex but your mind may work differently.
If you like the content on this site, please support it by using this link to order from Amazon. You know you were going to go there and buy stuff anyhow so why not help me pay the hosting bill.
Thanks so much for posting this, everything else Ive looked at is like do some algorithm on paper and that takes too long. This is great!
((Decimal to Binary)):–
I’m little bit of confused on how to write the table;; why did you start from 128 instead of 256. –+– and what about those binary digits which have 0 in first instead of 1.
So there’s a pattern to it. If you look at it written like this. Solve this problem from right to left.
128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
So if you add them all up, it adds up to 255.
Now for the second part of your question. Think of the zero as a place holder. Let’s keep it simple and use one digit:
8 + 4 + 2 + 1
0 1 0 1
So if there is a 1, you add that digit, if there is a zero, you don’t. In the case above, your hex number is 5. There is a zero under the ‘8’ location so that bit is turned off and it does not get added. One more for good measure:
128 + 64 + 32 +16 + 8 + 4 + 2 + 1
0 0 1 1 0 0 0 1
If you are trying to convert that binary number, you should read this as:
32 + 16 + 1 = 49 (in decimal)
If you want the hex value for the above you would read it like this:
8 + 4 + 2 + 1 + 8 + 4 + 2 + 1
0 0 1 1 0 0 0 1
0 + 0 + 2 + 1 = 3
0 + 0 + 0 + 1 = 1
0011 0001 = 31 (in hex)
Hopefully I didn’t just confuse you more. Let me know when it clicks.
This is a nice tip
But how can you convert decimal numbers bigger then 255 to binary?
Thanks in advance,
Hi Quinten, just add more bits…
2048 + 1024 + 512 + 256 + 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
thanks…