Advertisement
Guest User

Untitled

a guest
Sep 3rd, 2015
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.66 KB | None | 0 0
  1. /* char = 1 byte */
  2. typedef unsigned char byte;
  3.  
  4. /* for third method */
  5. union {
  6. float f;
  7. byte b[4];
  8. } c;
  9.  
  10. int main(){
  11. /* 35.34 = b01000010000011010101110000101001 IEE754 */
  12. /* 35.34 = 0x420d5c29 IEE754 */
  13.  
  14. byte bytes[4];
  15. /* MSB */
  16. bytes[3] = 0x42;
  17. bytes[2] = 0x0d;
  18. bytes[1] = 0x5c;
  19. bytes[0] = 0x29;
  20.  
  21.  
  22. /* normal bitshift method */
  23. unsigned int a1 = bytes[0]<<24;
  24. a1 |= (bytes[1]<<16);
  25. a1 |= (bytes[2]<<8);
  26. a1 |= bytes[3];
  27.  
  28. float a = (float)a1;
  29.  
  30.  
  31. /* memcpy method */
  32. float b = 0;
  33. memcpy(&b, &bytes, sizeof(b));
  34.  
  35.  
  36. /* union */
  37. c.b[3] = bytes[3];
  38. c.b[2] = bytes[2];
  39. c.b[1] = bytes[1];
  40. c.b[0] = bytes[0];
  41.  
  42. printf("a = %f\nb = %f\nc = %f\n",a,b,c.f);
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement