title: Print binary representation of a float number in C++
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2746380/float-to-binary-in-c">Float to binary in C++</a>  </p>
  </blockquote>
  
  
  
  <p>I want to print out the binary representation of a float number in C++. Not very practical, just out of curiosity.</p>
  
  <p>The following program doesn't compile though. The reinterpret_cast fails. What kind of cast can I use so that I can do the " &amp;(1 &lt;&lt; i) " part?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  
  void toBinary(float num) {
    int numi = reinterpret_cast&lt;int&gt;(num);
    cout &lt;&lt; num &lt;&lt; " " &lt;&lt; numi &lt;&lt; endl;
    for (int i = 0; i &lt; 8 * sizeof(num); i++){
      if (numi &amp; (1&lt;&lt;i)) {
        cout &lt;&lt; 1;
      } else {
        cout &lt;&lt; 0;
      }
    }
    cout &lt;&lt; endl &lt;&lt; endl;
  }
  
  int main() {
    float a;
    cout &lt;&lt; sizeof(int) &lt;&lt; " " &lt;&lt; sizeof(float) &lt;&lt; endl;
    a = 13.5;
    toBinary(a);
    toBinary(13.9);
    toBinary(2 * a);
    toBinary(-a);
  }
  </code></pre>