Convert Float Point value As Binary - C++ Data Type

C++ examples for Data Type:float

Description

Convert Float Point value As Binary

Demo Code

#include <cstdlib>
#include <iostream>
using namespace std;
void printBinary(const unsigned char val) {
   for(int i = 7; i >= 0; i--)
      if(val & (1 << i))
   std::cout << "1";
else/*from  w w w  . j  a va2s  .  c  o  m*/
   std::cout << "0";
}
int main(int argc, char* argv[]) {
   double d = 123123.123123;
   unsigned char* cp = reinterpret_cast<unsigned char*>(&d);
   for(int i = sizeof(double); i > 0 ; i -= 2) {
      printBinary(cp[i-1]);
      printBinary(cp[i]);
   }
}

Result


Related Tutorials