3. Decimal to binary conversion:
Method of division by two
Decimal to binary conversion is based on the fact that a decimal number can be
represented as
d=an2^n+a(n-1)2^(n-1)+a(n-2)2^(n-2).........a(1)2^(1)+a(0)2^(0)
Eg 9=1*2^3 + 0*2^2 + 0*2^1 + 1*2^0
If we divide it by two we get the quotient as
q=d/2=a(n)2^(n-1)+a(n-1)2^(n-2)+a(n-2)2^(n-3).........+a(1)
and remainder r =a0(a0 is the least significant digit of the binary number.)
We again divide the quotient by two the
q/2=d/4=a(n)2^(n-2)+a(n-1)2^(n-3)+a(n-2)2^(n-4).........+a(2)
and the remainder =a1
4. Method of division by 2 continued
We continue this process until the quotient becomes zero.
Eg 19=d= 1*2^4 + 0*2^3 + 0*2^2 + 1*2^1 + 1*2^0
q=d/2= 1*2^3 + 0*2^2 + 0*2^1 + 1*2^0
r1=1
q/2=d/2*2=1*2^2 + 0*2^1 + 0*2^0
r2=1
q/2*2=d/2*2*2= 1*2^1 + 0*2^0
r3=0
q/2*2*2=d/2*2*2*2=1*2^0
r4=0
q/2*2*2*2=d/2*2*2*2*2=0
r5=1
Thus the binary equivalent of 19 is 10011.
5. Division by 2 algorithm
var
D:integer {D is the decimal integer that is to be converted to binary};
B:bitstring{B stores the binary equivalent of D};
Q,R:integer{intermediate variables used in conversion};
input D;
b:null;
if(D=0) then begin B:=0 goto 10 end;
while (D!=0)do
begin
Q:=D div 2 {D div 2 gives the quotient of D/2}
R:=D mod 2;
B=concatenate to left (R,B);
D:=Q
end;
10:Output B;
end{of algorithm}
6. Converting decimal fraction to binary
To convert a decimal fraction to binary we use repeated division on the
integral part and repeated multiplication on the decimal part. Suppose we
want to convert (11.625)10 binary . We find the binary equivalent of 11 by
repeated division and binary equivalent of 625 by repeated multiplication.
The process of repeated multiplication is explained below.
Step 1. Multiply the decimal part by 2. The whole number part of the result
becomes the first number to right of the decimal point .
.625*2=1.25
so the first number to the right is 1
step 2 . We take the decimal part of the result and again multiply it by two .
.25*2=.050
So the second number to the right is 0.
7. Converting decimal fraction to binary continued
step 3. We take the decimal part of the result and again multiply it by 2
.50*2= 1.00
So the third number to the right of the decimal point is 1.
Step.4 There is no need for step 4 as the fractional part has become
zero.
So our answer becomes 1011.101
8. Converting decimal fraction to binary :Algorithm
var
D:real {D is the decimal fraction to be converted to binary};
B:Bitstring {Binary equivalent of D};
P:real {P is a intermediate variable used during conversion};
INTP:Bit{integer part of 9 which can either be 0 or 1};
begin {of algorithm}
input D;
B:=0 .null {null is a null string};
if(D=0) then begin B:=0; goto 10 end;
while(D!=0) and length (B)<=9 do
{length (B) returns the length of B and we have limited it to 9}
begin
P:=D*2;
INTP:=Trunc(P){Trunc P returns the integer part of P};