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

Write a program that: converts a float into its base 2 scientific notation addit

ID: 3558925 • Letter: W

Question

Write a program that: converts a float into its base 2 scientific notation

additional specification:

You may assume that all inputs will be valid. There is no need for error checking.

The only header file you may #include is iostream.

The format of your output must match mine exactly. We use diff to compare your output to mine to determine your

score.

Unsigned integer numbers.

5.1. You may assume that the unsigned integers will fit into a 64-bit unsigned long long.

5.2. The possible bases are 2-16 inclusive. For bases 11 to 16, you should use the letters A-F for digits valued greater

than 9.

5.3. Hint: Use a base 10 representation as an intermediate form for all combination of bases.

Signed integer numbers.
6.1. The input format will be [-][0-9]+, with no leading zeroes.

Float.

7.1. IEEE 32 bits (float): 31 sign bit, 23-30 exponent (with 127 offset), 0-22 mantissa (with implicit leading 1).

7.2. Your mantissa should have the implicit leading 1 prepended.

7.3. Hint: To make use of the bitwise operators you will need to convert your float to an integer. I used

Explanation / Answer

Program Code:


#include "stdafx.h"
#include <iostream>
#include<string>
#include <sstream>
using namespace std;

typedef union
{
    float f;
    unsigned int unint;
} FltUn;

void binaryCnvrt(float f)
{
    FltUn x;
    unsigned wk=0;
    x.f = f;
    wk = x.unint;
  
    unsigned bit = wk & 0x07FFFFF;
    std::cout << "1.";
    for(int i = 0; i< 23 ; ++i)
   {
        bit <<=1;
        cout << (bit & 0x0800000 ? '1' : '0');
    }
    cout << " E";
    int exp = (wk >> 23) & 0x0FF;
    exp -= 127;
  
    int i = 0;
    while((exp & 0x080) == 0 && i < 8)
   {
        exp <<= 1;
        ++i;
    }
    if(i == 8)
        cout << '0';
    for(;i< 8 ; ++i)
   {
        cout << (exp & 0x080 ? '1' : '0');
        exp <<=1;
    }
    cout << endl;
}

int main()
{
    float f;
    cout << "Enter a float value:"<< endl;
   cin>>f;
   stringstream ss (stringstream::in | stringstream::out);
    ss << f;
   string str = ss.str();
   char ch=str.at(0);
   if(ch=='-')
   {
       cout << "Output: "<<"-";
       binaryCnvrt(f);
       cout << endl;
   }
   else
   {
       cout << "Output: ";
       binaryCnvrt(f);
       cout << endl;
   }
   system("pause");
   return 0;
}

Sample Output:

Enter a float value:
-2.22
Output: -1.00011100001010001111011 E1

Press any key to continue . . .

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote