Hex binary decimal conversion tutorial

by
Share this Article: Facebook0Google+0Twitter18LinkedIn0Reddit0StumbleUpon1

Take this number:  343

What’s this number? You think it’s decimal by default, right? However, the truth is, we can’t be sure. We can say that 3 is the MSB (most significant bit) and we end at 3 (LSB) the less significant bit. We start counting from the Zero position. Zero has a value, the starting value. So from 0 to 10 there are 11 numbers.

We start from the LSB powered by the position. eg 3 * 10^0 = 3 * 1 = 3,    4 * 10^1 = 40 and 3 * 10 ^ 2 = 300 . Sum them and you have = 343. This is a decimal number because it has 10 digits: 0 1 2 3 4 5 6 7 8 9 10

The octal has 8 digits: 0 1 2 3 4 5 6 7 and the hexadecimal has 16 digits: 1 2 3 4 5 6 7 8 9 but from now on I can’t write 10 because 10 is consisted of two digits 1 and 0, so they invented another symbol with the value of 10, another one with the value of 11, another one with the value of 13 etc… A (10), B(11), C(12), D(13), E(14), F(15). As you can see, we say that’s a hex (16 digits) but the delimiter is not 16 but 16-1 = 15 because we start counting from Zero (0 – 15 -> 16 digits).

Binary to Decimal

In binary we have 2 digits: 0 and 1. The first position is 0, the second position is 1, the third position is 2 etc and the base power is number 2 (the number of digits). For example: 0011 0010 this can be written omitting the 00 (no value at the end of the number). Whatever is the power, the multiplication 0 * 2^8 = 0. There is no point to take care of them. So, the number is 110010. What’s this? Just do the math only where there’s 1 (don’t care about the 0).

2 + 16 + 32 = 50 (decimal)  To do this fast, you need to memorize all the powers of 2 from 0 to 16:

  • 2^0 = 1
  • 2^1 = 2
  • 2^2 = 4
  • 2^3 = 8
  • 2^4 = 16
  • 2^5 = 32
  • 2^6 = 64
  • 2^7 = 128
  • 2^8 = 256
  • 2^9 = 512
  • 2^10 = 1024
  • 2^11 = 2048
  • 2^12 = 4096
  • 2^13 = 8192
  • 2^14 = 16384
  • 2^15 = 32768
  • 2^16 = 65536

The pattern here is that the next number is double the previous. There are many questions based on sub-netting here. So you must be able to know all these powers of 2. Before the Cisco exams, be sure to take down all the numbers just in case you need them later — once you enter the class, you write down anything you want. That’s how we convert a binary to decimal.

Decimal to Binary

Let’s see the opposite: convert decimal to binary. First off, ask yourself: how many bits do I need? 8 bits? 9 bits? 10 bits?

The number is 343. Ok, you know that 2^9 is 512 (too large but does the job) and 2^8 is 256 (not enough room). So you need 2^9.  The power indicates the number of bits you need; yes 9 bits (from 0 to 8).

So, you look  for the next bigger number 512 > 343 and the power of 512 (2^9) indicates the number of bits you need (9 bits).

Let’s see something else: 2^4 = 16

What this means for us? It means that if I have 4 digits (4 bits) that can take 2 values (0 or 1) then the combinations are 16. Using 4 bits I can have 9 combinations. Using 9 bits, I can have 512 combinations. Let’s see these combinations:

  • 0000 // the first column is replacing 0 1 0 1 0 1 continuously
  • 0001 // the second column is repeating after 2 (11 00 11 00 etc)
  • 0010 // the third column is repeating after 4 (0000 1111 0000 1111 etc)
  • 0011 // the fourth column is repeating after 8 (00000000 11111111 etc)
  • 0100
  • 0101
  • 0110
  • 0111
  • 1000
  • 1001
  • 1010
  • 1011
  • 1100
  • 1101
  • 1110
  • 1111  -> 15  (remember: 16 – 1)

So, when someone says convert 343, you are looking for how many bits you need to store that number into a computer’s discrete memory. 2^8 = 256 combinations do not capable. 2^9=512 more than enough, so we end up using this option.

Once, Google Recruiter asked me: What’s the 2^21 ?

  • 2^16 = 65k
  • 2^17 = 128k
  • 2^18 = 260k
  • 2^19 = 512k;
  • 2^20 = 1m
  • 2^21 = 2m this is ~ the number of IPv4 address we have available to day.

So, to convert 343 to binary I need 9 bit. Every bit has a value. Write the value of its position for 9 bits:

256  128  64  32  16  8  4  2  1

Pay attention that 9 bits are from 0 to 8. So from 2^0 =1 to 2^8 = 256. Don’t be confused with 2^9 = 512. This is only due to the calculation of the combination (number of bits).

Let’s start from the MSB (look left):

  • 343 / 256 = 87 -> 1
  • 87 / 128 = nope -> 0
  • 87 / 64 = 23 -> 1
  • 23 / 32 = nope -> 0
  • 23 / 16 = 7 -> 1
  • 7 / 8 = nope -> 0
  • 7 / 4 = 3 -> 1
  • 3 / 2 = 1 -> 1
  • 1 / 1 = 1 -> 1

1 0101 0111  that’s the 343 number. This is how we convert it. The larger the number, the larger the number of bits we need to represent it. To verify it, all you have to  do is add all the powers at the “1” positions: 256 + 64 + 16 + 4 + 2 + 1 = 343. That’s all the binary / decimal conversions and vice versa.

Question: You have a decimal number consisted of 10 bits. I want you to tell me which combination is the 568th in decimal.

Okay, so, I have 10 bits. This means I have 2^10 combinations, aka 1024 combinations, starting from 0 to 1023. Who is in decimal the 568th combination? Answer: 567. Why? Remember that we start counting from the Zero, so the 1st combination is Zero, the second one is 1 … and so on.  Zero is not nothing, it has a value, a zero value!

Let’s see another exercise: Convert 87 to binary.

First of , ask yourself how many bits do I need for 87 representation in binary? 2^7 = 128, and 2^6 = 64. So, 7 bits would suffice. Let’s enumerate the value of its bit in every position from 0 bit to 7 bit:

128  64  32  16  8  4  2  1

  • 87/128 = nope (0)
  • 87 / 64 = 23 (1)
  • 23 / 32 = nope (0)
  • 23 / 16 = 7 (1)
  • 7 / 8 = nope (0)
  • 6 / 4 = 3 (1)
  • 3 / 2 = 1 (1)
  • 1 / 1 = 1 (1)

So the number is 0101 0111 or  1010111.

Another question: Convert 257 to binary:

Instead of figuring that I need 2^9 = 512, 9 bits. There is another tricky way to think this. I know that 2^8 = 256 combinations (from 0 to 255) which is 8 bits. So, I know that 255 = 1111 1111

If you think cleverly, 257 would be 255 + 2.

255 + 1 = 1 0000 0000

255 + 2 = 1 0000 0001 = 257 ß that’s our number. If you have the eye to observe these patterns, it’s quite easy to do the conversion. So, all you need is to remember standard powers of two and if the number requested is nearby, there you go ;)

Another question: Convert 127 to binary.

I know that 2^7 = 128 combinations which means from 0 – 127

The last combination (127) would be all “1”. So write 7 bits of “1” sequence:

1111 1111 (answer). Pretty simple, ah? Pay attention that of the number is even, the LSB (the right one) would be 0. Otherwise, the LSB would be “1”. For example, what about 511 ? I need 2^9 = 512 combinations from 0 to 511. The last combination would be with all bits “1”: 1 1111 1111

For example, what about 3? 2 bits, all of them “1”: 11. Is it easier for you to understand it? We have 4 combinations, 2^2 = 4

  • Number 0: 0000
  • Number 1: 0001
  • Number 2: 0010
  • Number 3: 0011  (answer)

Which is the number 255 in binary ?

Well, I know that 2^8 = 256, which means using 8 bits I can have 256 combinations, from 0 to 255. So, 255 is the last combination, and given what we previously said: 255 = 1111 1111  (8 bits all of them “1”).

Hex to Binary and vice versa

Hex stuff now… take a look here:

2^4 = 16 this means using 4  binary bits I can refer to 16 combinations, from 0 to 15, which means using 4 binary bits I can represent all the hex numbers. In octal-wise, 2^3 = 8, from 0 to 7. FYI, to indicate an octal I need 3 bits.

1111 1111 = 0xFF

the 0x indicates that a hex follows. Other way to do this is FFh (found in Assambly code).

To convert a Hex into binary: 0x3B2

Analyze it per digit:

  • 3 = 0011
  • B = 1011
  • 2 = 0010

Write them down altogether: 0011 1011 0011.

To find which decimal it is, do what we previously did:

512 + 256 + 128 + 32 + 16 + 2 = 946 (decimal).

Let’s do another one: Convert 00110010 into hex!

In hex, 0011 -> 3  and 0010 -> 2

So, in hex there’s : 0×32 , super easy, isn’t?

Hex to Decimals (pass through binary first) and vice versa

Just because hex to binary and binary to hex is so easy, we use this step before proceed to a hex to decimal or decimal to hex conversion. Otherwise you would have to use power of 16. That’s ok, but for my mind it’s more difficult to calculate 16^3 or 16^4 rather than converting it to binary first and into decimal then.

For questions please refer to our Q/A forum at : http://ask.unixmen.com/


Share this Article: Facebook0Google+0Twitter18LinkedIn0Reddit0StumbleUpon1