avr39-ripe

intFloatBytePointerMess

May 11th, 2019
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.77 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. int main()
  4. {
  5.     int i = 65535;
  6.     float f = 9.18341e-41;
  7.     char b[4]{ 255,255, 0, 0 };
  8.  
  9.     std::cout << "int as float " << *((float*) &i) << std::endl;
  10.     std::cout << "float as int "<< *((int*) &f) << std::endl;
  11.  
  12.     std::cout << "bytes as float " << *((float*) &b) << std::endl;
  13.     std::cout << "bytes as int " <<*((int*) &b) << std::endl;
  14.  
  15.     std::cout << std::endl;
  16.  
  17.     for (int count = 0; count < 4; count++)
  18.     {
  19.         std::cout << "b[" << count << "] == " << "i[" << count << "] ? " << (b[count] == ((char*)& i)[count]) << std::endl;
  20.     }
  21.  
  22.     std::cout << std::endl;
  23.  
  24.     for (int count = 0; count < 4; count++)
  25.     {
  26.         std::cout << "b[" << count << "] == " << "f[" << count << "] ? " << (b[count] == ((char*)& f)[count]) << std::endl;
  27.     }
  28.  
  29.     std::cout << std::endl;
  30. }
Advertisement
Add Comment
Please, Sign In to add comment