Binary to Real number conversion
The number system conversion is carried out for a binary numbers (base-2) into real number (base 10). The input binary bit sequence consists of integer and fraction binary bits separated by a decimal point. The integer bits and fractional bits are converted into decimal value separately and then combined to print real value as a result of given binary bit sequence.
The fractional part of binary bits are converted into decimal fraction value by multiplication of the bits by 2 power of negative positional value of the bits.
Binary to Real number Conversion - Manual Calculation
Given input binary numbers = 1110.1001, converts it into a real number. separate binary bits into integer binary bits and fractional binary bits by a delimiter . (point) binary bits (integer part) = 1110 binary bits (fraction part) = 1001 Binary bits (integer part) into decimal conversion binary bits (integer part) = 1110 1 x 2^3 + 1 x 2^2 + 1 x 2^1 + 0 x 2^0 1 x 8 + 1 x 4 + 1 x 2 + 0 x 1 8 + 4 + 2 + 0 1110 (base 2) = 14 (base 10)Binary bits into Decimal fraction conversion The fraction binary bits is multiplied by 2 power of negative bit position value. The position value order starts from right to left
Binary bits(fractional part) = 1001 = 1x 2^(-4) + 0x 2^(-3) + 0x 2^(-2) +1 x 2^(-1) = 1 x 0.0625 + 0 x 0.125 + 0x 0.25 +1 x 0.5 =0.5625combined results Given binary bits (base 2) = 1110.1001 real value (base 10)= 14.5625
Binary to Real number conversion - Java program
The Java param converts a binary bit sequence read from user into a real value consists of decimal value (integer) and fractional value.
import java.util.Scanner;
public class Binary2FDecimal {
public static int bin2dec(String bits) {
int decval=0;
int len = bits.length();
for(int n=0; n<len; n++ )
{
if ( bits.charAt(n) == '1' )
decval += Math.pow(2,len-n-1);
}
return decval;
}
public static double bin2frac(String bits) {
double fval=0;
int len = bits.length();
for(int n=0; n<len; n++ )
{
int bval = Integer.parseInt(bits.charAt(n)+"");
fval += bval * 1.0/Math.pow(2,n+1);
}
return fval;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Binary to Fractional decimal Conversion");
System.out.println("Enter binary bits with a decimal point");
String bits=sc.nextLine();
sc.close();
String parts[]=bits.split("\\.");
System.out.println("\nBinary decimal part : " + parts[0]);
int decval = Binary2FDecimal.bin2dec(parts[0]) ;
System.out.println("Decimal value : " + decval);
System.out.println("\nBinary fractional part : " + parts[1]);
double fval = Binary2FDecimal.bin2frac(parts[1]);
System.out.println("Fractional value : " + fval);
double dval = fval+decval;
System.out.println("Combined Double value : " + dval);
}
}
Binary to Real number conversion - Java code output
Binary to Fractional decimal Conversion Enter binary bits with a decimal point 11010.11010 Binary decimal part : 11010 Decimal value : 26 Binary fractional part : 11010 Fractional value : 0.8125 Combined Double value : 26.8125
Comments
Post a Comment