title>Mathematics - Binary Conversion
Binary Convertion

ny usual representation of a given quantity consists of a power series of a given base

as follows:

Q =  k=nΣk=0 akbk = anbn + an-1bn-1 + . . . + akbk + . . . + a2b2 + a1b1 + a0b0

where b is a fixed integer base, n, k are integers and a is an integer between 0 and b-1.
The number is written as:  anan-1 . . . ak . . a2a1a0

example: 236024569 decimal or 100110001 binary represent the following series:

2*108 + 3*107 + 6*106 + 0*105 + 2*104 + 4*103 +5*102 + 6*10 + 9
and
1*28 + 0*27 + 0*26 + 1*25 + 1*24 + 0*23 + 0*22 + 0*2 + 1     respectively.

For numbers less then one the exponent k become also negative and the number is represented as follows:

Q =  k=nΣk=-m akbk = anbn + . . . + akbk + . . . + a1b1 + a0b0 + a-1b-1 + a-2b-2 + . . . + a-mb-m


The integer part of the number is separated from its fractional part by a point.

To convert a decimal number to its binary equivalent you should find a maximal number given by a power of 2 which is closest to the given decimal number but does not exceed it. You do it by chosing an exponent n which satisfies the equation: 2n < D < 2n+1 where D is the decimal number to be converted.

Let's say our bigest possible exponent is n so the result is 2n. Then you write 1 and substruct it from the converted number. That gives you a reminder.

Then you take next lower exponenet n-1 which gives you the result 2n-1 and try to fit it to the reminder. If the number fits you write 1 if it is to big you write 0 and you proceed to the next exponent n-2.

You repeat the procedure until you get a remainder equal to 0 or you have enough positions after the binary point which should be written after the exponent becomes 0.

Our converter use exactly the same method but for manual convertion the methode is not very friendly. First you must remember several powers of two, then you must substract them to get reminders.

Let's have a look at the binary representation of a number again. Let take the integral part first.
Q =  k=nΣk=0 ak2k = an2n + an-12n-1 + . . . + ak2k + . . . + a222 + a12 + a0

where ak are equal to 0 or 1

Let's divide this expression by 2 and write down the reminder which is equal to a0 because this is only one which cannot be divided integrally.

The integral result of this division is presented below:

Q =  k=nΣk=1 ak2k = an2n + an-12n-1 + . . . + ak2k + . . . + a222 + a12

If we divide it by 2 we get

Q =  k=nΣk=2 ak2k = an2n + an-12n-1 + . . . + ak2k + . . . + a222 + a1

By repeating this we will get all factors   a0 , a1 , a2 , a3 . . . until the last one, when there will be nothing more to divide.

Let's do an example by converting 325 to binary.

324/2 = 162 r0=0 Let's write down all the reminders
r0 to r8
in the reverse order so we get:

101000100

which is the result of the convertion
162/2 = 81 r1=0
81/2 = 40 r2=1
40/2 = 20 r3=0
20/2 = 10 r4=0
10/2 =5 r5=0
5/2 = 2 r6=1
2/2 = 1 r7=0
1/2 = 0 r8=1

If there is a decimal fractional part of the number we treat it separetly but as you can easely discover we multiply it by 2 instead of dividing. We separate the integral parts and continuae multiply fractional. After we finish due to have obtained 0 in the fractional part or because we do not need more binary positions, we write down all integral parts after binary point. The convertion is done!

Let's do an example by converting 0.625 to binary.

0.625*2 = 1,250 I-1 = 1 Let's write down all the integral parts

.101

this is the result of the convertion
0.250*2 = 0,500 I-2 = 0
0.500*2 = 1.000 I-3 = 1


D some exercises an check the resulte with our converter.

Enjoy !

Witold W.