fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. union Float{
  5. // public:
  6. double m_Val;
  7. unsigned long long m_Hex;
  8. struct{
  9. unsigned long long mantissa:52;
  10. unsigned long long exponment:11;
  11. unsigned long long sign: 1;
  12. };
  13. // public:
  14. Float(double val=0.0) : m_Val(val){}
  15. void SetMantissa(unsigned long long m) {
  16. mantissa = m;
  17. }
  18. void SetExponment(unsigned long long e){
  19. exponment = e;
  20. }
  21. void SetSign(unsigned long long s){
  22. sign = s;
  23. }
  24.  
  25. double GetVal(){return m_Val;}
  26. unsigned long long GetHex(){return m_Hex;}
  27. };
  28.  
  29. int main()
  30. {
  31. Float f;
  32. f.SetExponment(0x400ULL);
  33. f.SetMantissa(0x8000000000000ULL);
  34. f.SetSign(0ULL);
  35.  
  36. cout << f.GetVal() << endl;
  37. cout << f.GetHex() << endl << endl;
  38.  
  39. f.m_Val = 0.5;
  40. cout << f.GetVal() << endl;
  41. cout << f.GetHex() << endl;
  42. cin.get();
  43. return 0;
  44. }
Success #stdin #stdout 0.01s 2684KB
stdin
Standard input is empty
stdout
3
4613937818241073152

0.5
4602678819172646912