//
25. Define a float variable. Take its address, cast that address
// to an unsigned char, and assign it to an unsigned char
// pointer. Using this pointer and [ ], index into the float
// variable and use the printBinary( )function defined in
// this chapter to print out a map of the float (go from 0 to
// sizeof(float). Change the value of the float and see if
// you can figure out what’s going on (the float contains
// encoded data).
// 这题要看书p189的例子
#include < iostream >
using namespace std;
void main() {
float fv;
unsigned char * p = reinterpret_cast < unsigned char *> ( & fv);
void printBinary( const unsigned char );
cout << " fv= " << fv << endl;
for ( int j = 0 ; j < sizeof ( float ); j ++ ){
cout << " p[ " << j << " ]: " ;
printBinary(p[j]);
cout << endl;
}
}
// : C03:printBinary.cpp {O}
void printBinary( const unsigned char val) {
for ( int i = 7 ; i >= 0 ; i -- )
if (val & ( 1 << i))
std::cout << " 1 " ;
else
std::cout << " 0 " ;
} /// :~
// to an unsigned char, and assign it to an unsigned char
// pointer. Using this pointer and [ ], index into the float
// variable and use the printBinary( )function defined in
// this chapter to print out a map of the float (go from 0 to
// sizeof(float). Change the value of the float and see if
// you can figure out what’s going on (the float contains
// encoded data).
// 这题要看书p189的例子
#include < iostream >
using namespace std;
void main() {
float fv;
unsigned char * p = reinterpret_cast < unsigned char *> ( & fv);
void printBinary( const unsigned char );
cout << " fv= " << fv << endl;
for ( int j = 0 ; j < sizeof ( float ); j ++ ){
cout << " p[ " << j << " ]: " ;
printBinary(p[j]);
cout << endl;
}
}
// : C03:printBinary.cpp {O}
void printBinary( const unsigned char val) {
for ( int i = 7 ; i >= 0 ; i -- )
if (val & ( 1 << i))
std::cout << " 1 " ;
else
std::cout << " 0 " ;
} /// :~