
Given either an "ordinary" floating point number or a bit pattern, it is easy to get the other representation, say, with a simple program like the following in C.
A similar program in Java is here.
four06% cat floats.c
/* floats.c: show bit representation for doubles */
#include <stdio.h>
#include <ctype.h>
void main(void){
union {
double d;
struct {
int p;
int q;
} b;
} r;
char ch;
for( ; ; ) {
while (isspace(ch = getchar()))
;
if (ch == 'x') {
scanf("%x %x", &r.b.p, &r.b.q);
printf(" Decimal: %20.16e\n",
r.d);
printf(" Bits: %08x %08x\n",
r.b.p, r.b.q);
}
else if (ch == 'f') {
scanf("%lf", &r.d);
printf(" Decimal: %20.16e\n",
r.d);
printf(" Bits: %08x %08x\n",
r.b.p, r.b.q);
}
else break;
}
}
Typical output looks like:
f -0.75 Decimal: -7.5000000000000000e-01 Bits: bfe80000 00000000 f 16.0 Decimal: 1.6000000000000000e+01 Bits: 40300000 00000000 f .75 Decimal: 7.5000000000000000e-01 Bits: 3fe80000 00000000 f 1.5 Decimal: 1.5000000000000000e+00 Bits: 3ff80000 00000000 x 3fe80000 00000000 Decimal: 7.5000000000000000e-01 Bits: 3fe80000 00000000 x bfffffff ffffffff Decimal: -1.9999999999999998e+00 Bits: bfffffff ffffffffFor the actual laboratory work:
four06% cat floating.c
/* floating.c: input a double, tear it into its component, reassemble.
* See Patterson & Hennessy, Comp. Org. & Design, pages 278-279
* for notation.
* Here Result = (-1)^Sign x (1 + Significand) x 2^(Exponent - Bias)
*/
#include <stdio.h>
#include <ctype.h>
/* this union allows one to extract bits from a double */
union double_tag{
double d;
struct {
unsigned int p;
unsigned int q;
} b;
};
void main(void){
union double_tag r; /* holds the input */
unsigned int Sign_mask = 0xA0000000; /* to extract Sign */
unsigned int Exponent_mask = 0x7FF00000; /* to extract Exponent */
unsigned int Signific_mask = 0x000FFFFF; /* to extract Signficand */
unsigned int Sign; /* the Sign */
unsigned int Exponent; /* the Exponent */
unsigned int Signific; /* the Signicand, as an integer */
double Significand; /* the Significand */
unsigned int Bias = 1023; /* the Bias */
double Result; /* the result of reassembling */
int BiasedExponent; /* the Biased Exponent == Exponent - Bias */
int i;
for( ; ; ) {
scanf("%lf", &r.d);
Sign = (r.b.p & Sign_mask) >> 31;
Exponent = (r.b.p & Exponent_mask) >> 20;
Signific = r.b.p & Signific_mask;
Significand = ( (double) Signific + r.b.q / 4294967295.0) / 1048576.0;
/* Note: the extra r.b.q / 4294967295.0 adds in the lower 32 bits. */
printf("(-1)^Sign x (1 + Significand) x 2^(Exponent - Bias) = Result\n");
Result = (Sign ? -1.0 : 1.0) * (1 + Significand);
BiasedExponent = Exponent - Bias;
if (BiasedExponent >= 0) {
for (i = 0; i < BiasedExponent; i++)
Result = Result * 2.0;
}
else {
for (i = 0; i < -BiasedExponent; i++)
Result = Result / 2.0;
}
printf("(-1)^%1i x (1 + %11.9f) x 2^(%4i - %4i) = %20.16f\n",
Sign, Significand, Exponent, Bias, Result);
}
}
ign x (1 + Significand) x 2^(Exponent - Bias) = Result
(-1)^0 x (1 + 0.000000000) x 2^(1027 - 1023) = 16.0000000000000000
.75
(-1)^Sign x (1 + Significand) x 2^(Exponent - Bias) = Result
(-1)^0 x (1 + 0.500000000) x 2^(1022 - 1023) = 0.7500000000000000
1.5
(-1)^Sign x (1 + Significand) x 2^(Exponent - Bias) = Result
(-1)^0 x (1 + 0.500000000) x 2^(1023 - 1023) = 1.5000000000000000
-2.0
(-1)^Sign x (1 + Significand) x 2^(Exponent - Bias) = Result
(-1)^1 x (1 + 0.000000000) x 2^(1024 - 1023) = -2.0000000000000000
ctrl-C