Question

Create a design document for a program that will take a number from the user and...

Create a design document for a program that will take a number from the user and convert it to IEEE single precision format. The program displays the IEEE representation (Single precision) of the number entered by the user.

PLEASE ADD THE PSEUDOCODE AND CODE WITH C PROGRAMMING

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Pseudocode:

All sections have been commented appropriately in regards to the explanation.

The entire process takes advantage of how the union data type works. Since the values stored in each of the members is the same as the first member of the union, the first member to be declared is the float number that we give as the input.

As for the Union data type, the ordering of the declaration is also important due to how the program assigns the values from the float to the unsigned integers. Since the size of the struct ieee is 32 bits, float is also taken as 32 bits. The values are then converted and stored by the program starting from LSB to MSB and distributed across the 3 sub members of res, namely significand, exponent and sign.

Code:

#include<stdio.h>
void printBinary(int n, int i)
{
int k;
int cur_digit;
for (k = i - 1; k >= 0; k--) {
cur_digit=n>>k;
if (cur_digit & 1)
printf("1");
else
printf("0");
}
/*NOTE: WE USE BITWISE OPERATORS HERE AS SINCE BITWISE OPERATORS OPERATE ON BIT VALUES ONLY IT SAVES US THE TROUBLE
OF TRYING TO MANUALLY CONVERT, THEY GET CONVERTED AUTOMATICALLY*/
/*PRINTBINARY HERE RIGHT SHIFTS AND THEN PERFORMS BITWISE AND OPERATION STARTING FROM THE MSB
HERE SMALL i REPRESENTS THE NUMBER OF BITS UNTIL WHICH WE WANT TO PRINT THE BINARY NUMBER TO
>> IS THE A BINARY RIGHT SHIFT OPERATOR WHICH SHIFTS THE FIRST OPERAND TO THE RIGHT BY NUMBER OF
DIGITS SPECIFIED BY THE SECOND OPERAND.SINCE i IS THE SIZE OF THE NUMBER ITSELF, SHIFTING BY i-1
GIVES US THE MSB OF THE NUMBER WHOSE BINARY REPRESENTATION WE ARE TRYING TO GET. THE OPERATOR &
IS ALSO A BITWISE OPERATOR, HERE THE cur_digit STORES THE BITWISE VALUES STARTING FROM MSB TO LSB.
PERFORMING & OPERATION WITH IT GIVES US "1" WHEN THE BIT VALUE WAS 1 ELSE "0", THUS PRINTING THE BIT VALUES OF
THE INPUT NUMBER ONE BY ONE STARTING FROM MSB TO LSB*/
   printf("|");
}
/*MEMBERS OF UNION SHARE MEMORY AND STORE SAME VALUES, AND THE SIZE OF THE UNION IS THE SAME AS THE SIZE OF THE LARGEST MEMBERS
HERE THE LARGEST MEMBER IS THE STRUCT IEEE THAT IS A TOTAL OF 32 BITS; AS FOR TYPEDEF, THE SYNTAX IS
TYPEDEF <ACTUAL DATA TYPE> <DERIVED DATA TYPE NAME (GIVEN BY USER)>, HERE num IS THE NAME OF THE UNION DATA TYPE, ORDERING IS IMPORTANT AND IS STORED
FROM LSB TO MSB*/
typedef union{
   float real;
   struct ieee{
       unsigned int significand : 23;//we use bit fields when we know maximum bits that will be used, significand has 23 bits
       unsigned int exponent : 8;/*exponent has 8 bits, and the exponent stored here is the biased exponent.
       Bias value is defined as (2^n)-1 for n bits, so it is +127 of true exponent value*/
       unsigned int sign : 1;//sign only requires 1 bit
       /*here the values that actually get sored in significand, exponent, and sign are already the values that correspond to the ieee format due
       them sharing the same memory, now as C will only output the numbers in decimal, that is base 10, all we need to do is output the binary
       equivalent stored at these places*/
   }res;
}num;
void getIEEE(num in)
{
printf("|%d|",in.res.sign);//as sign bit is only a single digit we can directly print it out
printBinary(in.res.exponent,8);
printBinary(in.res.significand,23);
//we need to convert exponent and significand to their binary equivalents
printf("\n");
}
int main()
{
num in;//stores the input and the result
   printf("Enter real number :");
   scanf("%f",&in.real);
   printf("Decimal equivalents of the number stored in memory are :\n");
   printf("Significand :%u\n",in.res.significand);
printf("Exponent :%u\n",in.res.exponent);
printf("Sign :%u\n",in.res.sign);
printf("IEEE754 form of %f is :\n",in.real);
getIEEE(in);
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Create a design document for a program that will take a number from the user and...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT