fork download
  1. // Online C++ compiler to run C++ program online
  2. #include <iostream>
  3. #include <stdint.h>
  4.  
  5. template <typename T>
  6. inline float getFloat(T, uint8_t *x)
  7. {
  8. T temp;
  9. uint8_t *p = &temp;
  10. for(uint8_t i = 0; i < sizeof(T); ++i)
  11. {
  12. *(p+i) = x;
  13. }
  14. return static_cast<float>(temp);
  15. };
  16.  
  17. union UN
  18. {
  19. uint16_t u16;
  20. uint8_t u8[2];
  21. struct
  22. {
  23. uint8_t b1;
  24. uint8_t b2;
  25. };
  26. void print() { printf("b1=%08b \nb2=%08b \nu16=%016b (%d, 0x%x)",
  27. b1, b2, u16, u16, u16); }
  28. };
  29.  
  30. int main() {
  31. UN un;
  32. un.b1 = 0x34;
  33. un.b2 = 0x12;
  34. un.print();
  35.  
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0.01s 5292KB
stdin
123
stdout
b1=%08b 
b2=%08b 
u16=%016b (52, 0x12)