#include #include #include using namespace std; void PrintBits(char letter) ; int GetBit(char * number, int bit); void PrintSign(char number[]) ; int GetAndPrintExponent(char number[]); void PrintBit(int bit); void PrintMagnitude(char number[], int exp); void PrintDenormal(char number[]); void PrintNumber(float x); int main(int argc, char * argv[]) { float x {numeric_limits::denorm_min()}; PrintNumber(x); x = numeric_limits::max(); PrintNumber(x); x = NAN; PrintNumber(x); while(1) { cout << endl << endl; cout << "Enter a number => "; cin >> x; PrintNumber(x); } return 0; } void PrintNumber(float x) { cout << "*************************************************" << endl; char * number; number = reinterpret_cast(&x); cout << endl; cout << "The number is: " << x << endl; cout << endl; cout << "The Bit Pattern in Memory is: "; for(int i = 31; i >= 0; --i) { cout << GetBit(number, i); } cout << endl << endl; cout << "The sign " << endl; PrintSign(number); cout << endl; cout << "The Exponenet" << endl; int exp = GetAndPrintExponent(number); cout << endl; cout << "The Magnitude" << endl; if (exp == -127) { PrintDenormal(number); } else { PrintMagnitude(number, exp); } cout << endl; } int GetAndPrintExponent(char number[]){ int value {0}; cout << "\tThe bit pattern is: "; for (int i = 30; i >= 23; --i) { int bit = GetBit(number, i); PrintBit(bit); value = value * 2 + bit; } cout << endl; cout << "\tThe base exponent is: " << value << endl; cout << "\tThe actual exponent is: " << value-127 << endl; return value - 127; } void PrintSign(char number[]) { int bit = GetBit(number, 31); cout << "\tThe bit is : " << bit << endl; cout << "\tThe sign is: " ; if (bit == 0) { cout << "+"; } else { cout << "-"; } cout << endl; } void PrintMagnitude(char number[], int exp) { float value = 1; float divide = .5; cout << "\tThe pattern is: " ; for(int i = 22; i >= 0; --i) { int bit = GetBit(number, i) ; PrintBit(bit); value += bit*divide; divide /= 2; } value *= pow(2, exp); cout << endl; cout << "\tThe value is: " << value << endl; } void PrintDenormal(char number[]){ float value = 0; float divide = .5; int exp = -126; cout << "\tThis is a denormal or subnormal value" << endl; cout << "\tThe pattern is: " ; for(int i = 22; i >= 0; --i) { int bit = GetBit(number, i) ; PrintBit(bit); value += bit*divide; divide /= 2; } value *= pow(2, exp); cout << endl; cout << "\tThe value is: " << value << endl; } void PrintBit(int bit) { if (bit == 0) { cout << "0"; } else { cout << "1"; } } int GetBit(char number[], int bit) { int place = bit / 8 ; int power = bit % 8; //cout << bit << " " << place << " " << power << endl; if(number [place] & 1<< power) { return 1; } else { return 0; } } void PrintBits(char letter) { for(int base = 7; base >= 0; --base){ if (1 << base & letter ) { cout << 1; } else { cout << 0; } } cout << " "; }