title: Showing binary representation of floating point types in C++
body: <p>Consider the following code for integral types:</p>
  
  <pre><code>template &lt;class T&gt;
  std::string as_binary_string( T value ) {
      return std::bitset&lt;sizeof( T ) * 8&gt;( value ).to_string();
  }
  
  int main() {
      unsigned char a(2);
      char          b(4);
  
      unsigned short c(2);
      short          d(4);
  
      unsigned int   e(2);
      int            f(4);
  
      unsigned long long g(2);
      long long h(4);
  
      std::cout &lt;&lt; "a = " &lt;&lt; +a &lt;&lt; " " &lt;&lt; as_binary_string( a ) &lt;&lt; std::endl;
      std::cout &lt;&lt; "b = " &lt;&lt; +b &lt;&lt; " " &lt;&lt; as_binary_string( b ) &lt;&lt; std::endl;
      std::cout &lt;&lt; "c = " &lt;&lt; c &lt;&lt; " " &lt;&lt; as_binary_string( c ) &lt;&lt; std::endl;
      std::cout &lt;&lt; "d = " &lt;&lt; c &lt;&lt; " " &lt;&lt; as_binary_string( d ) &lt;&lt; std::endl;
      std::cout &lt;&lt; "e = " &lt;&lt; e &lt;&lt; " " &lt;&lt; as_binary_string( e ) &lt;&lt; std::endl;
      std::cout &lt;&lt; "f = " &lt;&lt; f &lt;&lt; " " &lt;&lt; as_binary_string( f ) &lt;&lt; std::endl;
      std::cout &lt;&lt; "g = " &lt;&lt; g &lt;&lt; " " &lt;&lt; as_binary_string( g ) &lt;&lt; std::endl;
      std::cout &lt;&lt; "h = " &lt;&lt; h &lt;&lt; " " &lt;&lt; as_binary_string( h ) &lt;&lt; std::endl;
  
      std::cout &lt;&lt; "\nPress any key and enter to quit.\n";
      char q;
      std::cin &gt;&gt; q;
  
      return 0;
  }
  </code></pre>
  
  <p>Pretty straight forward, works well and is quite simple. </p>
  
  <hr>
  
  <p><strong>EDIT</strong></p>
  
  <p>How would one go about writing a function to extract the binary or bit pattern of arbitrary floating point types at compile time?</p>
  
  <hr>
  
  <p>When it comes to floats I have not found anything similar in any existing libraries of my own knowledge. I've searched google for days looking for one, so then I resorted into trying to write my own function without any success. I no longer have the attempted code available since I've originally asked this question so I can not exactly show you all of the different attempts of implementations along with their compiler - build errors. I was interested in trying to generate the bit pattern for floats in a generic way during compile time and wanted to integrate that into my existing class that seamlessly does the same for any integral type. As for the floating types themselves, I have taken into consideration the different formats as well as architecture endian. For my general purposes the standard IEEE versions of the floating point types is all that I should need to be concerned with.</p>
  
  <p>iBug had suggested for me to write my own function when I originally asked this question, while I was in the attempt of trying to do so. I understand binary numbers, memory sizes, and the mathematics, but when trying to put it all together with how floating point types are stored in memory with their different parts {sign bit, base &amp; exp } is where I was having the most trouble.</p>
  
  <p>Since then with the suggestions those who have given a great answer - example I was able to write a function that would fit nicely into my already existing class template and now it works for my intended purposes. </p>