Answer to Question 2.7
/* Copyright (c) 1990 by AT&T Bell Telephone Laboratories, Incorporated. */
/* The C++ Answer Book */
/* Tony Hansen */
/* All rights reserved. */
//: print out the exponent and mantissa of a double
#include <stream.h>
#include "2_7a.c" /* EXPAND */
void prdouble(double d)
{
double_byte db;
db.d = d;
cout << d << "\n";
cout << (db.s_m.sign ? "-" : " ");
cout << hex(db.s_m.exponent, 3) << " ";
cout << hex(db.s_m.mantissa, 1);
for (int i = 2; i < sizeof(double); i++)
cout << hex(db.c[i], 3);
cout << "\n";
}
//:2_7a.c
// union to pick apart the double
union double_byte
{
double d;
struct // first 2 bytes only
{
unsigned sign : 1;
unsigned exponent : 11;
unsigned mantissa : 4;
} s_m;
char c[sizeof(double)]; // all 8 bytes
};
Menu of Chapter 2 Answers
Answer to Question 2.8