Exercise 5. Based on the required reading, write a C program to implement your o
ID: 3735186 • Letter: E
Question
Exercise 5. Based on the required reading, write a C program to implement your own floating-point data-type, MyFloat. Recall that a float has 32-bits: 1 bit for sign, 8 bits for exponent, and 23 bits for significand/mantissa (plus 1 implicit bit). You may declare a struct for MyFloat with three data items: variable for sign-bit, dynamic array for exponent, and a dynamic array for significand. Your program must accept a:s command line arguments the number of bits to use for the exponent and significand (number of sign bits is always one). Similar to the last exercise, the program then asks the user for a signed decimal number, which is then converted to the internal bit representation of MyFloat. For example, assuming MyFloat is the same as float, i.e., 1 sign bit, 8 exponent bits, 23 signficand bits, the decimal number 103.2510 is equal to 0 10000101 10011101000000000000000 where green bit is the sign bit, blue bits are the exponent bits, and red bits are the significand bits.Explanation / Answer
PROGRAM CODE:--
#include<stdio.h>
#include<stdlib.h>
int conv_to_binary(int n, int i)
{
int k;
for (i--; i >= 0; i--)
{
k = n >> i;
if (k & 1)
printf("1");
else
printf("0");
}
}
typedef union
{
float fnum;
struct
{
unsigned int significand ;
unsigned int exponent ;
unsigned int sign_bit : 1;
} numobj;
} MyFloat;
int main(int argc, char *argv[])
{ int arg1, arg2;
//Convert commandline values to integers
arg1 = atoi(argv[1]);
arg2 = atoi(argv[2]);
//arg1 is the bits used for exponent and arg2 is the bits used for significand
MyFloat obj;
obj.numobj.significand = arg2;
obj.numobj.exponent = arg1;
printf(" Enter a signed decimal number: ");
scanf("%f",&obj.fnum);
printf("%d ",obj.numobj.sign_bit);
conv_to_binary(obj.numobj.exponent, arg1);
printf(" ");
conv_to_binary(obj.numobj.significand, arg2);
printf(" ");
return 0;
}