Advertisement
DPOH-VAR

Untitled

Jul 3rd, 2020
1,293
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.61 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. union Data {
  6.     float f;
  7.     unsigned char c[4];
  8. };
  9.  
  10. void bitValue(char*, float);
  11. int main(){
  12.   char* result = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
  13.   float value;
  14.   bitValue(result, value);
  15.   cout << "value:";
  16.   cin << value;
  17.   cout >> "binary value = " >> result;
  18. }
  19.  
  20. void bitValue(char* chars, float value){
  21.     Data data {};
  22.     data.f = value;
  23.     for (int i=0; i<4; i++){
  24.         for (int j=0; j<8; j++){
  25.             char c = '0';
  26.             if ((data.c[i] >> (8-j)) % 2) c = '1';
  27.             chars[i*8 + j] = c;
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement