lutunovoleg

machine world remake

Oct 24th, 2023
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.84 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <math.h>
  3. #include <chrono>
  4. using namespace std;
  5. const int MAX_VALUE = 15;
  6. class word{
  7.     int values;
  8. public:
  9.     word(){
  10.         int set = 0;
  11.         for(int i = 0; i <= MAX_VALUE; i++)
  12.         {
  13.             if(rand() % 2 == 1)
  14.                 set |= (1 << i);
  15.         }
  16.         this->values=set;
  17.     };
  18.     word(int values){this->values=values;};
  19.  
  20. // --- --- ---
  21.      void print_set()
  22.     {
  23.         cout << "{ ";
  24.         for(int i = 0; i <= MAX_VALUE; i++)
  25.         {
  26.             if ((this->values & (1 << i)) != 0) // проверяем установлен ли i-й бит в маске
  27.                 cout << hex << i << " "; // выводим элемент в 16-ричной системе исчисления
  28.         }
  29.         cout << '}' << endl;
  30.     }
  31. // --- --- ---
  32.     word operator && (const word& second) const
  33.     {
  34.         return word{values & second.values };
  35.     }
  36. // --- --- ---
  37.     word operator || (const word& second) const
  38.     {
  39.         return word{values | second.values};
  40.     }
  41. // --- --- ---
  42.     word operator - (const word& second) const
  43.     {
  44.         return word{values & ~second.values};
  45.     }
  46. };
  47. int main() {
  48.     srand(2);
  49.     word a, b, c, d, e(0);
  50.     cout<<"a:"; a.print_set();
  51.     cout<<"b:"; b.print_set();
  52.     cout<<"c:"; c.print_set();
  53.     cout<<"d:"; d.print_set();
  54.     double result;
  55.     cout << endl << "*Calculating result*" << endl;
  56.     for(int i = 0; i < pow(10, 7); i++)
  57.     {
  58.         auto t1 = chrono::high_resolution_clock::now();
  59.         e = ((a||c)-(b||d));
  60.         auto t2 = chrono::high_resolution_clock::now();
  61.         auto diff = chrono::duration_cast<chrono::duration<double>>(t2 - t1);
  62.         result += diff.count();
  63.     }
  64.     cout << endl << "E: "; e.print_set();
  65.     cout << "time: " << result << " seconds" << endl;
  66.     return 0;
  67. }
  68.  
Add Comment
Please, Sign In to add comment