| Test bits: float | ||||||||||||||
| How would we test the bits of a float value? | ||||||||||||||
| float f = 1024; | ||||||||||||||
| int n; | ||||||||||||||
| for (n = 0; n < 32; n++) | ||||||||||||||
| if (f & (1 << n)) | ||||||||||||||
| printf ("1"); | ||||||||||||||
| else | ||||||||||||||
| printf ("0"); | ||||||||||||||
| Compile error! | ||||||||||||||
| Need a way to look at f as if it were int: | ||||||||||||||
| float f = 1024; | ||||||||||||||
| int n, i; | ||||||||||||||
| i = (int) f; | ||||||||||||||
| for (n = 0; n < 32; n++) | ||||||||||||||
| if (i & (1 << n)) | ||||||||||||||
| printf ("1"); | ||||||||||||||
| else | ||||||||||||||
| printf ("0"); | ||||||||||||||
| Can't use cast on f: value is converted to int. | ||||||||||||||
| Cast a pointer: | ||||||||||||||
| int * iptr = (int *)&f; | ||||||||||||||
| i = *iptr; | ||||||||||||||