import java.util.Scanner;
/**
 * A program to show the double and float representations
 * as they are stored in the computer memory
 *
 * @author CS 140
 */
public class NumberStringConversion {
	/**
	 * The main method showing that allows the user to input
	 * doubles and floats and shows the binary representation
	 * @param args command line not used
	 */
	public static void main(String[] args) {
		double inputD = 1;
		Scanner keyboard = new Scanner(System.in);
		while (inputD != 0) {

			System.out.println("Input a double:");
			inputD = keyboard.nextDouble();
			long longRepD = Double.doubleToRawLongBits(inputD);
			String strRepD =  Long.toBinaryString(longRepD);
			strRepD = "00000000000000000000000000000000" +
			"00000000000000000000000000000000" + strRepD;

			int len = strRepD.length();
			strRepD = strRepD.substring(len-64);
			System.out.println("Double Binary representation: " + strRepD);
			System.out.println("Fields:\n" +
					"\tSign="+strRepD.charAt(0) + " " +
					"Exp=" +strRepD.substring(1,12) + " " +
					"Frac=" + strRepD.substring(12));


			float inputF = (float)inputD;
			int intRepF = Float.floatToRawIntBits(inputF);
			String strRepF =  Integer.toBinaryString(intRepF);
			strRepF = "00000000000000000000000000000000" + strRepF;
			len = strRepF.length();
			strRepF = strRepF.substring(len-32);
			System.out.println("Float Binary representation: " + strRepF);
			System.out.println("Fields:\n" +
					"\tSign="+strRepF.charAt(0) + " " +
					"Exp=" + strRepF.substring(1,9) + " " +
					"Frac=" +strRepF.substring(9));
		}
		keyboard.close();
	}

}