B
bauran
In Thinking in C++ by Bruce eckel, there is a program given to print a
double value
in binary.(Chapter 3, page no. 189)
int main(int argc, char* argv[])
{
if(argc != 2)
{
cout << "Must provide a number" << endl;
exit(1);
}
double d = atof(argv[1]);
unsigned char* cp = reinterpret_cast<unsigned char*>(&d);
for(int i = sizeof(double); i > 0 ; i -= 2)
{
printBinary(cp[i-1]);
printBinary(cp);
}
}
assume printbinary() prints (const unsigned char) in binary i.e. 8
bits. Moreover I haven't provided header files to shorten code. Please
assume proper header files and namespaces are included.
Here while printing cp when i=8(assuming double is of 8 bytes),
wouldn't it be undefined behaviour?
I mean this code doesn't work as it doesn't print cp[0].
Bruce Eckel is a very renowned writer. How can he do this type of
mistake?
double value
in binary.(Chapter 3, page no. 189)
int main(int argc, char* argv[])
{
if(argc != 2)
{
cout << "Must provide a number" << endl;
exit(1);
}
double d = atof(argv[1]);
unsigned char* cp = reinterpret_cast<unsigned char*>(&d);
for(int i = sizeof(double); i > 0 ; i -= 2)
{
printBinary(cp[i-1]);
printBinary(cp);
}
}
assume printbinary() prints (const unsigned char) in binary i.e. 8
bits. Moreover I haven't provided header files to shorten code. Please
assume proper header files and namespaces are included.
Here while printing cp when i=8(assuming double is of 8 bytes),
wouldn't it be undefined behaviour?
I mean this code doesn't work as it doesn't print cp[0].
Bruce Eckel is a very renowned writer. How can he do this type of
mistake?