Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have included code which produces hexadecimal output that I think may help you understand floating-point numbers. Here is an example:</p> <blockquote> <p>double: 00 00 A4 0F 0D 4B 72 42 (1257096936000.000000) (+0x1.24B0D0FA40000 x 2^40)</p> </blockquote> <p>From my code example below, it should become obvious to you how to output the bits. Cast the double's address to <code>unsigned char *</code> and output the bits of <code>sizeof(double)</code> chars.</p> <p>Since I want to output the exponent and significand (and sign bit) of a floating-point number, my example code digs into the bits of the IEEE-754 standard representation for 64-bit 'double precision' floating pointing point in radix 2. Therefore I do not use <code>sizeof(double)</code> other than to verify that the compiler and I agree that <code>double</code> means a 64-bit float.<br></p> <p>If you would like to output the bits for a floating-point number of any type, do use <code>sizeof(double)</code> rather than <code>8</code>.</p> <pre><code>void hexdump_ieee754_double_x86(double dbl) { LONGLONG ll = 0; char * pch = (char *)&amp;ll; int i; int exponent = 0; assert(8 == sizeof(dbl)); // Extract the 11-bit exponent, and apply the 'bias' of 0x3FF. exponent = (((((char *)&amp;(dbl))[7] &amp; 0x7F) &amp;lt;&amp;lt; 4) + ((((char *)&amp;(dbl))[6] &amp; 0xF0) &amp;gt;&amp;gt; 4) &amp; 0x7FF) - 0x3FF; // Copy the 52-bit significand to an integer we will later display for (i = 0; i &amp;lt; 6; i ++) *pch++ = ((char *)&amp;(dbl))[i]; *pch++ = ((char *)&amp;(dbl))[6] &amp; 0xF; printf("double: %02X %02X %02X %02X %02X %02X %02X %02X (%f)", ((unsigned char *)&amp;(dbl))[0], ((unsigned char *)&amp;(dbl))[1], ((unsigned char *)&amp;(dbl))[2], ((unsigned char *)&amp;(dbl))[3], ((unsigned char *)&amp;(dbl))[4], ((unsigned char *)&amp;(dbl))[5], ((unsigned char *)&amp;(dbl))[6], ((unsigned char *)&amp;(dbl))[7], dbl); printf( "\t(%c0x1.%05X%08X x 2^%d)\n", (((char *)&amp;(dbl))[6] &amp; 0x80) ? '-' : '+', (DWORD)((ll &amp; 0xFFFFFFFF00000000LL) &amp;gt;&amp;gt; 32), (DWORD)(ll &amp; 0xFFFFFFFFLL), exponent); } </code></pre> <p>Nota Bene: The significand is displayed as a hexadecimal fraction ("0x1.24B0D0FA40000") and the exponent is display as decimal ("40"). For me, this was an intuitive way to display the floating-point bits.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload