Guest User

Untitled

a guest
Jan 20th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.45 KB | None | 0 0
  1. double d = 1234.5678;
  2. uint64_t bits;
  3. memcpy(&bits, &d, sizeof bits);
  4. printf("the representation of %g is %08"PRIX64"n", d, bits);
  5.  
  6. union { double d; uint64_t i; } u;
  7. u.d = 1234.5678;
  8. printf("the representation of %g is %08"PRIX64"n", d, u.i);
  9.  
  10. void *my_memcpy(void *dst, const void *src, size_t n) {
  11. unsigned char *d = dst;
  12. const unsigned char *s = src;
  13. for (size_t i = 0; i < n; i++) { d[i] = s[i]; }
  14. return dst;
  15. }
  16.  
  17. double d = 1234.5678;
  18. void *p = malloc(sizeof(double));
  19. if (p != NULL) {
  20. uint64_t *pbits = memcpy(p, &d, sizeof(double));
  21. uint64_t bits = *pbits;
  22. printf("the representation of %g is %08"PRIX64"n", d, bits);
  23. }
  24.  
  25. double d = 1234.5678;
  26. void *p = malloc(sizeof(double));
  27. assert(p);
  28. uint64_t *pbits = memcpy(p, &d, sizeof(double));
  29. uint64_t bits = *pbits;
  30. printf("the representation of %g is %08"PRIX64"n", d, bits);
  31.  
  32. double d = 1234.5678;
  33. uint64_t bits;
  34. memcpy(&bits, &d, sizeof bits);
  35. printf("the representation of %g is %08"PRIX64"n", d, bits);
  36.  
  37. double d = 1234.5678;
  38. uint64_t bits;
  39. memcpy(&bits, &d, sizeof bits);
  40. printf("the representation of %g is %08"PRIX64"n", d, bits);
  41.  
  42. union { double d; uint64_t i; } u;
  43. u.d = 1234.5678;
  44. printf("the representation of %g is %08"PRIX64"n", d, u.i);
  45.  
  46. double d = 1234.5678;
  47. unsigned char bits[sizeof(d)];
  48. memcpy(&bits, &d, sizeof(bits));
  49. printf("the representation of %g is ", d);
  50. for(int i=0; i<sizeof(bits); i++) {
  51. printf("%02x", (unsigned int) bits[i]);
  52. }
  53. printf("n");
Add Comment
Please, Sign In to add comment