GastonFontenla

Bitmask

Oct 6th, 2018
249
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. #define SB(mask, k) (mask |= (1 << k))
  4. #define TB(mask, k) (mask ^= (1 << k))
  5. #define GB(mask, k) ((bool)(mask & (1 << k)))
  6.  
  7. using namespace std;
  8.  
  9. void SetBit(int &mask, const int &k)
  10. {
  11.     mask = mask | (1 << k);
  12. }
  13.  
  14. void ToggleBit(int &mask, const int &k)
  15. {
  16.     mask = mask ^ (1 << k);
  17. }
  18.  
  19. bool GetBit(int &mask, const int &k)
  20. {
  21.     return mask & (1 << k);
  22. }
  23.  
  24. void ShowMask(int &mask)
  25. {
  26.     cout << "Mask: ";
  27.     for(int i=31; i>=0; i--)
  28.     {
  29.         cout << GB(mask, i);
  30.     }
  31.     cout << endl;
  32. }
  33.  
  34. int main()
  35. {
  36.     int mask = 0;
  37.     int consultas;
  38.     cin >> consultas;
  39.  
  40.     for(int i=0; i<consultas; i++)
  41.     {
  42.         int pos;
  43.         cin >> pos;
  44.         TB(mask, pos);
  45.         ShowMask(mask);
  46.     }
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment