/* Single-Precision Floating-Point Numbers Just like we learned in class, floating-point numbers are stored in the IEEE 754 format. Write a C program that reads a 32-bit single-precision binary number and prints its value using the "%g" formatting inside printf. HINT: It may be easier to read the values of the individual bits as characters, and calculate the sign-bit, the 8 exponent bits, and the 23-bit mantissa bits separately. ============== Sample Output 1 =============== Enter a single-precision binary number: 00111110001000000000000000000000 The value entered is: 0.15625 ============================================== ============== Sample Output 2 =============== Enter a single-precision binary number: 11000011110010010001000000100000 The value entered is: -402.126 ============================================== ============== Sample Output 3 =============== Enter a single-precision binary number: 01000000011000000000000000000000 The value entered is: 3.5 ============================================== */ #include #include #include int main(int argc, char *argv[]) { // Declare variables char c; int sign, counter; double exponent=0, mantissa=1, placeVal; // Gather input from user printf("Enter a single-precision binary number:\n\n"); scanf("%c", &c); // PROCESS SIGN BIT: If sign bit= 0 then 1, else -1 sign = (c=='0')?1:-1; // PROCESS EXPONENT: While scanning in the 8 exponent values, if // a value is 1, it adds the value of that place to the exponent // variable. Exponent was initialized to - to accumulate correct value for(counter=0, placeVal=128; counter<8; counter++, placeVal/=2) { scanf("%c", &c); if(c=='1') exponent += placeVal; } exponent = pow(2, exponent-127); // PROCESS MANTISSA: While scanning in the 23 mantissa values, if // a value is 1, it adds the value of that place to the mantissa // variable. Mantissa was initialized to 1 to accumulate correct value for(counter=0, placeVal=0.5; counter<23; counter++, placeVal/=2) { scanf("%c", &c); if(c=='1') mantissa += placeVal; } // Prints the result printf("\nThe value entered is: %g\n", sign*exponent*mantissa); return 0; }