Pages

শুক্রবার, ১৪ ডিসেম্বর, ২০১২

How to convert fractional binary to decimal

Algorithm:


Algorithm to convert the fractional binary to decimal or floating point binary to decimal:

Step1. First we convert the integral part of binary number to decimal. For this we multiply each digit separately from right side by 1, 2, 4, 8, 16 … respectively then add them this is integral part of decimal number.

Step2. Now we convert the fractional part of binary number to decimal. For this we multiply each digit separately from left side by 1/2, 1/4, 1/8, 1/16 … respectively then add them this is fractional part of decimal number.

Step3: Add the integral and fractional part of decimal number.

Example for floating point binary to decimal:

For example we want to convert the binary number 101111.1101 to decimal number.

Step1: Conversions of 101111 to decimal:

S1:  1 * 1 = 1
S2:  1 * 2 = 2
S3:  1 * 4 = 4
S4:  1 * 8 = 8
S5:  0 * 16 = 0
S6:  1 * 32 = 32
Integral part of decimal number: 1 + 2 + 4+ 8+ 0+ 32 = 47
Step2: Conversions of .1101 to decimal:

S1: 1 * (1/2) = 0.5
S2: 1 * (1/4) = 0.25
S3: 0 * (1/8) = 0
S4: 1 * (1/16) = 0.0625
Fractional part of decimal number = 0.5 + 0.25 + 0 + 0.0625 = 0.8125
So equivalent binary number is: 0.101100
Step 3: So binary value of binary number 101111.1101 will be 47 + 0.8125 = 47.8125
 
#include<stdio.h>
int main()
{
 char frabinary[1000];
 int i,n,array[1000],fraarray[1000]={0};
 int sum=0,t=1;
 double s=0.5,frasum=0.0,result;
 printf("Enter the number:");
 scanf("%s",&frabinary);
 i=0,n=0;
 while(frabinary[i]!='.')
 {
     array[i]=frabinary[i]-48;
     n=n*10+array[i];
     i++;
 }
 while(frabinary[i+1]!='\0')
 {
     fraarray[i]=frabinary[i+1]-48;
     frasum=frasum+(double)fraarray[i]*s;
     s=s*(0.5);
     i++;
 }
 while(n!=0)
 {
     sum=sum+(n%10)*t;
     n=n/10;
     t=t*2;
 }
 result=sum+frasum;
 printf(" convertion result is %f",result);
 return 0;
}
 

0 comments:

একটি মন্তব্য পোস্ট করুন