Advertisement
Guest User

Untitled

a guest
Apr 21st, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. struct Bin
  6. {
  7.     unsigned int b0 : 1;
  8.     unsigned int b1 : 1;
  9.     unsigned int b2 : 1;
  10.     unsigned int b3 : 1;
  11.     unsigned int b4 : 1;
  12.     unsigned int b5 : 1;
  13.     unsigned int b6 : 1;
  14.     unsigned int b7 : 1;
  15. };
  16.  
  17. struct Hex
  18. {
  19.     unsigned int h0 : 4;
  20.     unsigned int h1 : 4;
  21. };
  22.  
  23. struct Oct
  24. {
  25.     unsigned int o0 : 3;
  26.     unsigned int o1 : 3;
  27.     unsigned int o2 : 3;
  28. };
  29.  
  30. union MyByte
  31. {
  32. private:
  33.     Bin bin;
  34.     Hex hex;
  35.     Oct oct;
  36.     unsigned char data;
  37.  
  38. public:
  39.     MyByte(unsigned char byte) : data(byte) {}
  40.     void PrintBin()
  41.     {
  42.         cout << bin.b7 <<
  43.                 bin.b6 <<
  44.                 bin.b5 <<
  45.                 bin.b4 <<
  46.                 bin.b3 <<
  47.                 bin.b2 <<
  48.                 bin.b1 <<
  49.                 bin.b0 << endl;
  50.     }
  51.     void PrintHex()
  52.     {
  53.         //std::hex - переключение в 16-тиричную запись
  54.         cout << std::hex << hex.h1 << hex.h0 << endl;
  55.         cout << std::dec; // переключаемся в режим десятичной записи
  56.     };
  57.     void PrintOct()
  58.     {
  59.         cout << std::oct << oct.o0 << oct.o1 << oct.o2 << endl;
  60.         cout << std::dec;
  61.     }
  62. };
  63.  
  64. int main()
  65. {
  66.     MyByte byte(0xF0);
  67.     byte.PrintBin();
  68.     byte.PrintHex();
  69.     byte.PrintOct();
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement