15 May 2010

Binary to decimal & viceversa conversion

This technique is useful while solving networks problems.

1. Binary to decimal
Select the first value from left hand side. If its 0, next; if its 1, we start.
If its succeeded by a 0, then double the number (i.e. 1 in this case).
If its succeeded by a 1, then double the number and add 1 to it.

Carry out the same procedure for all digits.

for eg. 11010110
1=>1
(1*2)+1=3          since followed by 1.
3*2=6                 since 0.
(6*2)+1=13       since 1.
(13*2)=26         since 0.
(26*2)+1=53    since 1.
(53*2)+1=107  since 1.
(107*2)=214    since 0.

final answer => 214.

2. Decimal to binary
This is little tricky but not that much you usually do.
Remember the range 128, 64, 32, 16, 8, 4, 2, 1.
The range specifies the position of bits.
For any bit to be 1 or 0, compareit with the remainder, if remainder larger than value put 1 at that position, else put 0.


select any no. say 189
1st  bit=>    is 189 >= 128 ?  yes.      So =>1                           189-128=61
2nd bit=>    is 61   >=   64 ?    no.      So =>0
3rd  bit=>    is 61   >=   32 ?  yes.      So =>1                            61-32=29
4th  bit=>    is 29   >=   16 ?  yes.      So =>1                            29-16=13
5th  bit=>    is 13   >=     8 ?  yes.      So =>1                            13-8=5
6th  bit=>    is 5     >=     4 ?  yes.      So =>1                             5-4=1
7th  bit=>    is 1     >=     2 ?  no.       So =>0
8th  bit=>    is 1     >=    1 ?  yes.      So =>1                              1-1=0

final answer => 10111101

Cross checking : Always remember, the remainder must be zero, then the conversion is correct.

No comments:

Post a Comment