// Code to show binary representation of a floating point number // Author: Sanjiv K. Bhatia // Requires an argument between 0 and 1 (a floating point number) #include int main ( int argc, char ** argv ) { using namespace std; if ( argc < 2 ) { cerr << "usage: " << argv[0] << " mantissa" << endl; return ( 1 ); } int i ( 0 ); float d = atof ( argv[1] ); // Get the number from command line if ( d >= 1.0 ) { cerr << "Mantissa should be less than 1.0" << endl; return ( 1 ); } while ( d > 0 && i < 23 ) // Stop after 23 bits; no exponent { if ( ( d *= 2 ) >= 1.0 ) { cout << '1'; d -= 1.0; } else cout << '0'; i++; } cout << endl; return ( 0 ); }