Advertisement
obernardovieira

Manipulate bits

Apr 4th, 2016
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.89 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::endl;
  5.  
  6. #define     MAX_COLUMNS 3
  7. #define     MAX_ROWS    3
  8. #define     MAX_BITS    3
  9.  
  10. void move_down(int m[MAX_COLUMNS][MAX_ROWS], int x, int y) {
  11.    
  12.     static int last[MAX_ROWS]   =  { -1 };
  13.     static int p[MAX_ROWS]      =  { 0 };
  14.     static int n[MAX_ROWS]      =  { 0 };
  15.    
  16.     if(last[x + n[x]] == -1)
  17.         last[x + n[x]] = m[x + n[x]][y];
  18.     else
  19.         m[x + n[x]][y] = last[x + n[x]];
  20.    
  21.     if(p[x + n[x]] == MAX_BITS) {
  22.         m[x + n[x]][y] = last[x + n[x]];
  23.         p[x + n[x]] = 0;
  24.         n[x] ++;
  25.     }
  26.        
  27.     m[x + n[x]][y] |= (0b1 << p[x + n[x]]);
  28.     p[x + n[x]] ++;
  29.    
  30. }
  31.  
  32. void print_m(int m[MAX_COLUMNS][MAX_ROWS]) {
  33.    
  34.     int y;
  35.    
  36.     for(int x = 0; x < MAX_COLUMNS; x++) {
  37.        
  38.         for(y = 0; y < MAX_ROWS; y++) {
  39.             cout << m[x][y] << ", ";
  40.         }
  41.         cout << endl;
  42.     }
  43.    
  44. }
  45.  
  46. int main()
  47. {
  48.     /*
  49.     int matrix[MAX_COLUMNS][MAX_ROWS] = {
  50.         {0b001, 0b010, 0b011, 0b100, 0b101, 0b110},
  51.         {0b010, 0b011, 0b100, 0b101, 0b110, 0b101},
  52.         {0b011, 0b100, 0b101, 0b110, 0b101, 0b100},
  53.         {0b100, 0b101, 0b110, 0b101, 0b100, 0b011},
  54.         {0b101, 0b110, 0b101, 0b100, 0b011, 0b010},
  55.         {0b110, 0b101, 0b100, 0b011, 0b010, 0b001}
  56.     };
  57.     */
  58.    
  59.     int matrix[MAX_COLUMNS][MAX_ROWS] = { 0 };
  60.    
  61.     for(int a = 0; a < 6; a++) {
  62.        
  63.         move_down(matrix, 0, 0);
  64.         print_m(matrix);
  65.         cout << endl << endl;
  66.     }
  67.    
  68.    
  69.     int
  70.         a = 0b0001,
  71.         b = 0b0101,
  72.         c = 0b1000;
  73.        
  74.     cout << (a & b) << endl;
  75.     cout << (a | b) << endl;
  76.     cout << (a ^ b) << endl;
  77.     cout << (a & 0b1) << endl;
  78.     cout << (b & 0b101) << endl;
  79.     cout << (b & (0b1 << 2) ) << endl;
  80.     cout << (c & (0b1 << 3) ) << endl;
  81.    
  82.     cout << "Ola!" << endl;
  83.    
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement