Write a program in JAVA to read in a 32-bit pattern and translate to a IEEE-754
ID: 3883179 • Letter: W
Question
Write a program in JAVA to read in a 32-bit pattern and translate to a IEEE-754 floating point number. Do not use library routines. Be sure to look for infinity, -infinity, zero and NaN patterns. ( This is what I NEED, please use the import: import javax.swing.JOptionPane; for the input and outputs, a string to turn the int to binary,then a loop to check and see if there are any special patterns, example: NaN, -infinity, infinity, if there isnt any special patterns check with something else "LIKE" this code below, not exactly just an example
sign=(inputString.CharAt(0)=='0'?1:-1);
String expString=inputString,substring(1,8);
String mainString=inputSting.substring(9)
Explanation / Answer
import javax.swing.JOptionPane;
public class IEEE_754_2 {
public static void main(String[] args) {
String inputString,ieee_754;
inputString = JOptionPane.showInputDialog("Enter the 32bit number");
//In the 32 bit First digit is sign, next 8 are the exponent and finally, the last 23 are the significand.
//Floating point number is calculated by sign * 2^exp * Significand
if(null!=inputString && inputString.length()==32 && inputString.matches("[01]{32}")){
int sign=(inputString.charAt(0)=='0'?1:-1);//0 is postive
String expString=inputString.substring(1,9);
String mainString=inputString.substring(9);
int intExp = Integer.parseInt(expString, 2);
intExp = intExp -127;
double fraction =0.0;
for(int index=0; index <mainString.length();index++){
fraction = fraction + Integer.parseInt(""+mainString.charAt(index))*Math.pow(2,-1*(index+1));
}
ieee_754=""+(fraction+1)*sign*Math.pow(2,intExp);
JOptionPane.showMessageDialog(null,ieee_754);
}
else{
JOptionPane.showMessageDialog(null,"Please Enter Valid Input");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.