mathajax

Real number to Binary conversion

The number system conversion is carried out for a real number to binary number bits. The input real number consists of integer and fraction values separated by a decimal point. The integer value and fractional value are converted into binary separately and then combined to print binary bits as a result of the real number.

The fractional decimal number are converted into binary numbers by successive multiplication of the fraction by 2 and resulting string of integer digits is the converted binary numbers.


Real number to Binary Conversion - Manual Calculation

       Given input real number= 14.125, convert it into binary numbers

       real   = integer . fraction  

       separate real into integer and fractional part   

       decimal  = 14
       fraction = .125

     Decimal value (integer) into Binary conversion 

       2 | 14  | 0
       -----------
       2 | 7   | 1 
       -----------
       2 | 3   | 1 
       -----------
          1   

    decimal value (base 10) 14 =  1110   (binary base 2)

  Fractional value into Binary conversion  
    

The fraction value is multiplied by 2 and result has a integer (1 or 0) and fraction value. the integer as a binary bit and the faction value used for next iteration. The process repeated until the fraction value reached to 0.

fraction = .125 = .125 x 2 = 0.250 x 2 = 0.50 x 2 = 1.0 fraction = 0.125 = 100

combined results given real value (base 10)= 14.125 into binary bits (base 2) = 1110.100


Real number to Binary Conversion - Java program

 
import java.util.Scanner;
public class FractionDecimal {
  
 public static String dec2bin(int decval)
 {  
  StringBuffer sbuf =new StringBuffer();  
  while(decval>1) 
  {
   sbuf.append( decval % 2 ) ;
   decval = decval /2;      
  }    
  if (decval==1)
   sbuf.append(1);
  
  return sbuf.reverse().toString();
 }
 
 public static String frac2bin(double fval)
 {  
  StringBuffer sbuf =new StringBuffer();  
  while(fval != 0) 
  {
   fval = fval * 2;
   sbuf.append(Math.floor(fval)) ;
   fval = (fval>=1) ? fval-1 : fval;      
  }    
    
  return sbuf.reverse().toString();
 }

 public static void main(String[] args) {
  
    Scanner sc=new Scanner(System.in);
    System.out.println("Fractional decimal to Binary Conversion");
  
  System.out.println("Enter double value");  
  double val=sc.nextDouble();
  sc.close();
  
  
  int  decpart = (int) val;
  double fracpart = (val - decpart);
  
  StringBuffer bits = new StringBuffer();
  String ibits =  FractionDecimal.dec2bin(decpart);
  String fbits =  FractionDecimal.frac2bin(fracpart);

  bits.append(ibits);bits.append('.');
  bits.append(fbits);
  
  
  System.out.println("\nDecimal value :" + decpart);
  System.out.println("Binary :" + ibits);
  
  System.out.println("\nFractional value :" + fracpart);
  System.out.println("Binary :" + fbits);
  
  System.out.println("\nDecimal with Fraction number :" + val);
  System.out.println("Binary :" + bits);  
 }
}

Real number to Binary Conversion - Java program Output


Fractional decimal to Binary Conversion
Enter double value
13.625

Decimal value :13
Binary :1101

Fractional value :0.625
Binary :101

Decimal with Fraction number :13.625
Binary :1101.101

Comments

Popular posts from this blog

Solving System of Linear Equations by Gauss Jordan Elimination

Matrix Forward and Back Substitution

Solve System of Linear Equations by LU Decompose

Chebyshev distance between two points

Binary 1's and 2's Complement