Binary <—> Decimal Conversion

There are some cool tricks to doing this, which I can never remember, so I'll list the methods that are more intuitive. 

Binary to Decimal - very easy - you just need to know how much each digit place is worth (see table below).  For larger values not in the table, use a calculator to raise 2 to the power of that position (multiply 2 times itself that many times).  So, for example, 1101 has ones in the positions of 8, 2, and 1 for a total of 11.  You just take each "1", figure it's decimal equivalent, and add the values up.

  Byte 1 Byte 0
  Nibble 3 Nibble 2 Nibble 1 Nibble 0
Digit Position  15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Binary Digit's Weight in Decimal 32768 16384 8192 4096 2048 1024 512 256 128 64 32 16 8 4 2 1

 

Decimal to Binary - a bit tougher, but not too bad - you need to work your way from left to right.  We will assign the letter "X" to the Decimal value for this method:

  1. find the leftmost binary bit value (the decimal weight) that is less than or equal to X
  2. record a 1 in that position
  3. subtract that weight to yield a new value Y   Check to see if Y=0 - if so you are done !!
  4. move to the right until you find a weight that is less than Y
  5. go back to Step 2

Example, Convert Decimal 134 to Binary:  

    Refer to the weights as you go through the steps:

Position   

8   

7   

6   

5   

4   

3   

2   

1   

0

Weight   

256   

128   

64   

32   

16   

8   

4   

2   

1

    Decimal 134  =  Binary 10000110