/* display bits of a float
and
input bit pattern and display float value */
/* Between each power of 2 there are 2^23 evenly spaced floats
(spaced 1/2^24 * 2^exp apart) thus eg. there are 2^23 floats
between 1/2 and 1 and also 2^23 floats between 1 and 2 and between
128 and 256 and between 4096 and 8192 and between 1/128 and 1/64 etc.
Floats are uniformly distributed between powers of 2. but
Half of the (positive) floats are between 0 and 1
0 is actually roughly 2^-127 (mantissa of all 0)
Largest is roughly 2^127 */
/* several bit patterns are nan
some are Inf */
#include
#include
main(int argc, char **argv) {
float f;
int i;
unsigned long *p=(unsigned long *)&f;
unsigned long j;
char c, again;
/* investigate -0: acts like a negative number: -0*5=-0, -0*-0=0 -0=0
printf("-0=%f -0*-0=%f -0*5=%f -0==0? %d\n",f,f*f,f*5,f==0);
*/
printf("You enter a real number, the program shows the floating point representation.\n");
printf("You enter 32 0's and 1's, the program shows what f.p. number that is\n");
do {
printf("\nEnter a real number: ");
scanf("%f",&f);
//printf("%f %9.7f %e %ul %ul\n",f,f,f,(unsigned long int)f,*p);
// f=1/0/(1/0); runtime error
f = sqrt(-1);
for (i=31; i>=0; i--) {
printf("%d",(*p>>i)&1);
if (i==31 || i==23)
putchar(' ');
}
putchar('\n');
putchar('\n');
*p = 0;
getchar();
printf("\nEnter 32 bits:\n"); /*ok to enter blanks*/
for (i=31; i>=0; i--) {
while ((c=getchar()) == ' ')
;
/* j = (getchar()=='0' ? 0 : 1);*/
j = (c=='0' ? 0 : 1);
*p = (j<