Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

cp3 - Notepad File Edit Format View Help The binary representation of decimal Th

ID: 3726638 • Letter: C

Question

cp3 - Notepad File Edit Format View Help The binary representation of decimal The binary representation of decimal The binary representation of decimal The binary representation of decimal The binary representation of decimal The binary representation of decimal The binary representation of decimal The binary representation of decimal The binary representation of decimal The binary representation of decimal is 0 10001100 00010001110110000001011 is 0 01111011 10011001100110011001100 8763.011 25 -38 1234567890 is 01001001100101100000001011010010 is 1 10000010 0110 is 1 10000010 10101000011110101110000 is 0 10001100 00010001110110000000000 is 1 10001100 00010001110110000000001 -13. 265 8763. 000001 -8763.001

Explanation / Answer

/*
Copy the below code and save this and make one input file cp3.txt
and run it you will generate the output file cp3.out.txt as required
*/

#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>

int WriteBinaryNos(int n, int i)
{
   freopen("cp3.out.txt", "a+",stdout);
    int k;
    for (i--; i >= 0; i--)
   {
      k = n >> i;
      if (k & 1)
          printf("1");
      else
         printf("0");
    }
}


/*
this has been used for specifying the format of the floating point number after getting converted into the single precision IEEE floating point format
so here I make a union for specifying the number and a structure for its representation as IEEE floating point format
for output in as format as it is specified we set the bits of the integer variable
here unsigned int MANTISSA:23; means that MANTISSA is a integer variable with 23 bits and same for exponent ana sign bit

this will work as the decimal or floating point number is store in the system as 2s complement 32 bitformat so ,what we do is to extract those bits with the help of shift right use
example : 25 in system stored as 00000000000000000000000000011001
here first bit is sign bit and remaining 31 bits are of the number

likewise same is for floating point format
representation of -11.0 is 1 10000010 01100000000000000000000(sign+exponent+mantissa)
which we get by right shift

*/


typedef union
{
      float f;
      struct
      {
            unsigned int mantissa : 23;
            unsigned int exponent : 8;
            unsigned int sign : 1;
       } field;
} myfloat;


typedef union{
   int i;
   struct
   {
      unsigned int otherpart : 31;
      unsigned int sign : 1;
   } field;
}myint;


void ConverDecimal2Binary(char number[]){
        int flag=0;
       int length;
       length=strlen(number);
       int count=0;
       while(length--)
       {
           if(number[length]=='.')
           {
               flag=1;
              
           }

           if(flag==1){
               count++;
           }
       }

       freopen("cp3.out.txt", "a+",stdout);
       printf("The binary representation of the decimal");         
      
       if(flag)
           {
               printf(" %16s            is ",number );
               myfloat var;
               var.f = atof(number);
               printf("%d ",var.field.sign);
               WriteBinaryNos(var.field.exponent, 8);
               printf(" ");
               WriteBinaryNos(var.field.mantissa, 23);
            }
       else
           {
               printf("%16s              is ",number );
               myint input;
               input.i = atoi(number);
               printf("%d",input.field.sign );
               WriteBinaryNos(input.field.otherpart,31);
           }
   
}

//reading the input from file
void ReadNos(){
           freopen("cp3.txt", "r",stdin);
           char number[16];
           while(scanf("%s",number)!=EOF){
               ConverDecimal2Binary(number);
               printf(" ");
           }
       }

int main()
{  

           ReadNos();
           return 0;
}