Exercise 3-25 (Print bits of float and double in C/C++)

Chapter_3     Exercise_3-24     FloatLimits Exercise_3-26







Exercise 3-25     TCP1, p. 229


Exercise 3-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 (Chapter_3) 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).




CONTENTS:     floatbinrev.c     FloatBinRev.cpp




Note:  See also ch3-FloatBinary.




floatbinrev.c         download


// float binary reversed
#include <stdio.h> // for printf(), putchar()
#include <stdlib.h> // for atof(), exit()

void printBinary(const unsigned char val); // Display a byte in binary
void printBinRev(const unsigned char val); // reversed
void printData(unsigned char*, int); // Display a data type in binary
void printDataRev(unsigned char*, int); // reversed

int main(int argc, char* argv[])
{
if(argc != 2)
{
printf("Must provide a number\n");
exit(1); // out of main(), end program; return value 1 signals an error
}
double d = atof(argv[1]);
float f = d;
unsigned char* cp = (unsigned char*)(&f);
printData(cp, sizeof(float)); // cp[3], cp[2], cp[1], cp[0]
printDataRev(cp, sizeof(float)); // cp[0], cp[1], cp[2], cp[3]

cp = (unsigned char*)(&d);
printData(cp, sizeof(double)); // cp[7], cp[6], ..., cp[1], cp[0]
printDataRev(cp, sizeof(double)); // cp[0], cp[1], ..., cp[6], cp[7]

return 0; // normal return value for main(), signals no errors
}

void printBinary(const unsigned char val) // Display a byte in binary
{
int i, bit = 128; // 2^7

for(i = 7; i >= 0; i--)
{ // print bits from first (most significant) to last (least significant)
if(val & bit) // set (1) bit
{putchar('1');}
else {putchar('0');} // 0 bit
bit >>= 1; // bit /= 2;
}
}
void printBinRev(const unsigned char val) // Display byte (reversed)
{
int i, bit = 1;

for(i = 0; i <= 7; i++)
{ // print bits from last (least significant) to first (most significant)
if(val & bit) // set (1) bit
{putchar('1');}
else {putchar('0');} // 0 bit
bit <<= 1; // bit *= 2;
}
}

void printData(unsigned char* cp, int size) // Display a data type in binary
{ // sizeof(float) = 4: cp[3], cp[2], cp[1], cp[0]
for(; size > 0; size--)
{printBinary(cp[size-1]);}
putchar('\n');
}
void printDataRev(unsigned char* cp, int size) // Display data type (reversed)
{ // sizeof(float) = 4: cp[0], cp[1], cp[2], cp[3]
int i;

for(i = 0; i < size; i++)
{printBinRev(cp[i]);}
putchar('\n');
}
/*
gcc floatbinrev.c -o floatbinrev
./floatbinrev
Must provide a number

./floatbinrev 0 1
Must provide a number // exactly one number

./floatbinrev 0
00000000000000000000000000000000 // single-precision floating-point
00000000000000000000000000000000 // reversed
0000000000000000000000000000000000000000000000000000000000000000 // double
0000000000000000000000000000000000000000000000000000000000000000 // reversed

./floatbinrev 1
00111111100000000000000000000000
00000000000000000000000111111100
0011111111110000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000111111111100

./floatbinrev 2
01000000000000000000000000000000
00000000000000000000000000000010
0100000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000010

./floatbinrev 3
01000000010000000000000000000000
00000000000000000000001000000010
0100000000001000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000001000000000010

./floatbinrev 4
01000000100000000000000000000000
00000000000000000000000100000010
0100000000010000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000100000000010

./floatbinrev 0.1
00111101110011001100110011001101
10110011001100110011001110111100
0011111110111001100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110011001110111111100

./floatbinrev 0.2
00111110010011001100110011001101
10110011001100110011001001111100
0011111111001001100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110011001001111111100

./floatbinrev -0.1
10111101110011001100110011001101
10110011001100110011001110111101
1011111110111001100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110011001110111111101

./floatbinrev -.2
10111110010011001100110011001101
10110011001100110011001001111101
1011111111001001100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110011001001111111101

./floatbinrev 15
01000001011100000000000000000000
00000000000000000000111010000010
0100000000101110000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000111010000000010

./floatbinrev 15.1
01000001011100011001100110011010
01011001100110011000111010000010
0100000000101110001100110011001100110011001100110011001100110011
1100110011001100110011001100110011001100110011000111010000000010

./floatbinrev 15.2
01000001011100110011001100110011
11001100110011001100111010000010
0100000000101110011001100110011001100110011001100110011001100110
0110011001100110011001100110011001100110011001100111010000000010

./floatbinrev 15.3
01000001011101001100110011001101
10110011001100110010111010000010
0100000000101110100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110010111010000000010
*/











FloatBinRev.cpp         download


// float binary reversed
#include <cstdlib> // for atof(), exit()
#include <iostream>
using std::cout;
using std::endl;

void printBinary(const unsigned char val); // Display a byte in binary
void printBinRev(const unsigned char val); // reversed
void printData(unsigned char*, int); // Display a data type in binary
void printDataRev(unsigned char*, int); // reversed

int main(int argc, char* argv[])
{
if(argc != 2)
{
cout << "Must provide a number" << endl;
exit(1); // out of main(), end program; return value 1 signals an error
}
double d = atof(argv[1]);
float f = d;
unsigned char* cp = reinterpret_cast<unsigned char*>(&f);
printData(cp, sizeof(float)); // cp[3], cp[2], cp[1], cp[0]
printDataRev(cp, sizeof(float)); // cp[0], cp[1], cp[2], cp[3]

cp = reinterpret_cast<unsigned char*>(&d);
printData(cp, sizeof(double)); // cp[7], cp[6], ..., cp[1], cp[0]
printDataRev(cp, sizeof(double)); // cp[0], cp[1], ..., cp[6], cp[7]

return 0; // normal return value for main(), signals no errors
}

void printBinary(const unsigned char val) // Display a byte in binary
{
for(int i = 7, bit = 128; i >= 0; i--) // 128 = 2^7
{ // print bits from first (most significant) to last (least significant)
if(val & bit) // set (1) bit
{cout << "1";}
else {cout << "0";} // 0 bit
bit >>= 1; // bit /= 2;
}
}
void printBinRev(const unsigned char val) // Display byte (reversed)
{
for(int i = 0, bit = 1; i <= 7; i++)
{ // print bits from last (least significant) to first (most significant)
if(val & bit) // set (1) bit
{cout << "1";}
else {cout << "0";} // 0 bit
bit <<= 1; // bit *= 2;
}
}

void printData(unsigned char* cp, int size) // Display a data type in binary
{ // sizeof(float) = 4: cp[3], cp[2], cp[1], cp[0]
for(; size > 0; size--)
{printBinary(cp[size-1]);}
cout << endl;
}
void printDataRev(unsigned char* cp, int size) // Display data type (reversed)
{ // sizeof(float) = 4: cp[0], cp[1], cp[2], cp[3]
for(int i = 0; i < size; i++)
{printBinRev(cp[i]);}
cout << endl;
}
/*
g++ FloatBinRev.cpp -o FloatBinRev
./FloatBinRev
Must provide a number

./FloatBinRev 0 1
Must provide a number // exactly one number

./FloatBinRev 0
00000000000000000000000000000000 // single-precision floating-point
00000000000000000000000000000000 // reversed
0000000000000000000000000000000000000000000000000000000000000000 // double
0000000000000000000000000000000000000000000000000000000000000000 // reversed

./FloatBinRev 1
00111111100000000000000000000000
00000000000000000000000111111100
0011111111110000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000111111111100

./FloatBinRev 2
01000000000000000000000000000000
00000000000000000000000000000010
0100000000000000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000000000000010

./FloatBinRev 3
01000000010000000000000000000000
00000000000000000000001000000010
0100000000001000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000001000000000010

./FloatBinRev 4
01000000100000000000000000000000
00000000000000000000000100000010
0100000000010000000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000000100000000010

./FloatBinRev 0.1
00111101110011001100110011001101
10110011001100110011001110111100
0011111110111001100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110011001110111111100

./FloatBinRev 0.2
00111110010011001100110011001101
10110011001100110011001001111100
0011111111001001100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110011001001111111100

./FloatBinRev -0.1
10111101110011001100110011001101
10110011001100110011001110111101
1011111110111001100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110011001110111111101

./FloatBinRev -.2
10111110010011001100110011001101
10110011001100110011001001111101
1011111111001001100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110011001001111111101

./FloatBinRev 15
01000001011100000000000000000000
00000000000000000000111010000010
0100000000101110000000000000000000000000000000000000000000000000
0000000000000000000000000000000000000000000000000111010000000010

./FloatBinRev 15.1
01000001011100011001100110011010
01011001100110011000111010000010
0100000000101110001100110011001100110011001100110011001100110011
1100110011001100110011001100110011001100110011000111010000000010

./FloatBinRev 15.2
01000001011100110011001100110011
11001100110011001100111010000010
0100000000101110011001100110011001100110011001100110011001100110
0110011001100110011001100110011001100110011001100111010000000010

./FloatBinRev 15.3
01000001011101001100110011001101
10110011001100110010111010000010
0100000000101110100110011001100110011001100110011001100110011010
0101100110011001100110011001100110011001100110010111010000000010
*/









Chapter_3     Exercise_3-24     FloatLimits BACK_TO_TOP Exercise_3-26



Comments

Popular posts from this blog

Contents