fork download
  1. #include <cstdio>
  2. using namespace std;
  3.  
  4. int main(void)
  5. {
  6. double *d = new double;
  7. *d = 15.25;
  8. printf("float: %f\n", *d);
  9.  
  10. //forced hex output of double
  11. printf("forced bitwise of double: ");
  12. unsigned char * c = (unsigned char *) d;
  13. int i;
  14. for (i = sizeof (double)-1; i >=0 ; i--) {
  15. printf ("%02X ", c[i]);
  16. }
  17. printf ("\n");
  18.  
  19. //cast to long long-pointer, so that bitops become possible
  20. long long * l = (long long*)d;
  21. //now the bitops:
  22. printf("IntHex: %016X, float: %f\n", *l, *(double*)l); //this output is wrong!
  23. *l = *l | 0x07;
  24. printf("last 3 bits set to 1: %016llX, float: %.30f\n", *l, *d);//this output is wrong!
  25. *l = *l | 0x18;
  26. printf("2 bits more set to 1: %016llX, float: %.30f\n", *l, *d);//this output is wrong!
  27.  
  28. *l = *l | 0x10000000000ll;
  29. printf("float4: %f\n", *d);
  30. }
  31.  
Success #stdin #stdout 0.01s 2856KB
stdin
Standard input is empty
stdout
float: 15.250000
forced bitwise of double: 40 2E 80 00 00 00 00 00 
IntHex: 0000000000000000, float: 0.000000
last 3 bits set to 1: 402E800000000007, float: 15.250000000000012434497875801753
2 bits more set to 1: 402E80000000001F, float: 15.250000000000055067062021407764
float4: 15.251953