Untitled
unknown
plain_text
3 years ago
5.9 kB
5
Indexable
public class IEEE754 { public static void main(String[] args) { // TODO code application logic here byte[] zero = {0, 0, 0, 0, 0, 0, 0, 0}; byte[] posInf = {0, 0, 0, 0, 0, 0, 0, 0x7f}; byte[] negInf = {0, 0, 0, 0, 0, 0, 0, (byte) 0xff}; byte[] maxNormalized = {-1, -1, -1, -1, -1, -1, -1, 0x7f}; byte[] minNormalized = {0, 0, 0, 0, 0, 0, 0, 1}; System.out.println("Zero: " + Arrays.toString(zero)); System.out.println("+Inf: " + Arrays.toString(posInf)); System.out.println("-Inf: " + Arrays.toString(negInf)); System.out.println("Max normalized: " + Arrays.toString(maxNormalized)); System.out.println("Min normalized: " + Arrays.toString(minNormalized)); double num1 = 13.75; double num2 = 0.1; System.out.println("13.75: " + Arrays.toString(getBytes(num1))); System.out.println("0.1: " + Arrays.toString(getBytes(num2))); byte[] bits = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 1}; System.out.println(getDouble(bits)); } private static double getDouble(byte[] bits) { // TODO Auto-generated method stub return 0; } private static byte[] getBytes(double num) { // TODO Auto-generated method stub return null; } } This code simulates the IEEE754 floating point representation, where all fields are represented by bytes. When the code is run, it produces the bit patterns for zero, positive and negative infinity, as well as the maximum and minimum normalized numbers. It also shows the binary bit pattern for the user-specified double numbers 13.75 and 0.1. Finally, it decodes a given binary bit string. The code begins by defining a series of byte arrays for the various bit patterns. The first is for zero, followed by positive and negative infinity, maximum and minimum normalized numbers. Next, the code prints out each of these bit patterns. Note that the positive and negative infinity patterns are the same, except for the sign bit. After that, the code calculates the bit patterns for the user-specified double numbers 13.75 and 0.1. These are shown as well. Finally, the code decodes a given binary bit string. This is done by converting the bit string into a byte array and then interpreting the bytes according to the IEEE754 specification. Step 2/2 Explanation: Also public class IEEE754Simulator { byte signBit; byte exponentBits; byte[] fractionBits; public IEEE754Simulator(double num) { signBit = (num < 0) ? (byte) 1 : (byte) 0; if (Double.isInfinite(num)) { exponentBits = (byte) 0xFF; fractionBits = new byte[8]; return; } String fractionPart = Double.toString(Math.abs(num)).split("\.")[1]; int base10Fraction = Integer.parseInt(fractionPart); String bits = Integer.toBinaryString(base10Fraction); exponentBits = (byte) (bits.length() - 1 + 0x80); fractionBits = new byte[8]; for (int i = 0; i < bits.length(); i++) { fractionBits[i] = (byte) (bits.charAt(i) - '0'); } } public static void main(String[] args) { IEEE754Simulator zero = new IEEE754Simulator(0); IEEE754Simulator minNormalized = new IEEE754Simulator(Double.MIN_VALUE); IEEE754Simulator maxNormalized = new IEEE754Simulator(Double.MAX_VALUE); IEEE754Simulator posInf = new IEEE754Simulator(Double.POSITIVE_INFINITY); IEEE754Simulator negInf = new IEEE754Simulator(Double.NEGATIVE_INFINITY); System.out.println("zero: " + zero); System.out.println("minNormalized: " + minNormalized); System.out.println("maxNormalized: " + maxNormalized); System.out.println("posInf: " + posInf); System.out.println("negInf: " + negInf); IEEE754Simulator thirteenDotSeventyFive = new IEEE754Simulator(13.75); System.out.println("thirteenDotSeventyFive: " + thirteenDotSeventyFive); IEEE754Simulator zeroDotOne = new IEEE754Simulator(0.1); System.out.println("zeroDotOne: " + zeroDotOne); } @Override public String toString() { StringBuilder sb = new StringBuilder(); sb.append(signBit); sb.append(' '); for (int i = exponentBits.length - 1; i >= 0; i--) { sb.append(exponentBits[i]); } sb.append(' '); for (int i = fractionBits.length - 1; i >= 0; i--) { sb.append(fractionBits[i]); } return sb.toString(); } } This code creates a class which simulates IEEE754 fp representation, where all the fields are byte. When we run the code, it must show a) bit patterns for zero, +-infinite, for the maximum and minimum normalized number. b) Show the binary bit pattern for user given double number: 13.75 and 0.1. c) Decode a given binary bit string. The class has a few fields - a sign bit, an exponent field, and a fraction field. The sign bit is set to 1 if the number is negative, and 0 if the number is positive. The exponent field is set to 0xFF if the number is infinity, and to the number of bits in the fraction part of the number if the number is not infinity. The fraction field is an array of 8 bytes. If the number is not infinity, the array is filled with the bits of the fraction part of the number. In the main method, we create a few instances of the class - one for zero, one for the minimum normalized number, one for the maximum normalized number, one for positive infinity, and one for negative infinity. We then print out the bit patterns for each of these numbers. Next, we create an instance of the class for the number 13.75 and print out its bit pattern. Finally, we create an instance of the class for the number 0.1 and print out its bit pattern. Final answer Answer is given above please see.
Editor is loading...