4.2.4 Visualizing floating point representation

This can done using a good debugger and a simple sample program. We’ll use the program in listing 1 as an example to find out the bit pattern of 0.2 as a single precision floating point number.


Listing 1:checkformat
 
1int main(void
2{ 
3  float f; 
4 
5  f = 0.2; 
6  return 0;  
7}

We compile the program using the command gcc -g -o test test.c to make sure the debug symbol table remains intact. Next, we invoke the debugger using the command gdb test. Once we enter the debugger, we use the command b 6 to insert a breakpoint on line 6.

Then, we use the command run in gdb to run the program. The programs stops on line 6. Here, we can examine the value of variable f as a 32-bit binary number using the commmand x/tw &f to examine the variable in binary.

We should see exactly the same bit pattern as table 3.