Advertisement
El_GEMMY

Bitmask operations

Jul 2nd, 2022
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.22 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3.  
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. typedef long long ll;
  8. typedef unsigned long long ull;
  9. typedef tree<int,null_type,less<>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
  10.  
  11. #define all(v) v.begin(),v.end()
  12. #define rall(v) v.rbegin(),v.rend()
  13. #define MOD 1000000007
  14. #define PI 3.14159265
  15. //#define ceil(a, b) ((a / b) + (a % b ? 1 : 0))
  16. #define imin INT_MIN
  17. #define imax INT_MAX
  18. #define nl '\n'
  19. #define modulo(a, b, m) ((a % m) * (b % m)) % m
  20.  
  21. void Start_Crushing() {
  22.     ios::sync_with_stdio(false);
  23.     cin.tie(nullptr);
  24.     cout.tie(nullptr);
  25. #ifndef ONLINE_JUDGE
  26.     freopen("input.txt", "r", stdin);
  27.     freopen("output.txt", "w", stdout);
  28. #endif
  29. }
  30. //vector<int> dx = {0, 0, 1, -1, 1, 1, -1, -1}, dy = {1, -1, 0, 0, 1, -1, 1, -1};
  31. //int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};
  32.  
  33. void solve(){
  34.     ll n, q, type; cin >> n >> q;
  35.  
  36.     while(q-- and cin >> type){
  37.  
  38.         if(type == 1){
  39.             for(int bit = 0; bit < 32; bit++){
  40.                 if(!(n & (1 << bit))){
  41.                     n |= (1 << bit);
  42.                     break;
  43.                 }
  44.             }
  45.             cout << n;
  46.         }else if(type == 2){
  47.             for(int bit = 0; bit < 32; bit++){
  48.                 if(n & (1 << bit)){
  49.                     n = n & ~(1 << bit);
  50.                     break;
  51.                 }
  52.             }
  53.             cout << n;
  54.         }else if(type == 3){
  55.             for (int bit = 0; bit < 32; bit++) {
  56.                 if (n & (1 << bit)) break;
  57.                 n |= (1 << bit);
  58.             }
  59.             cout << n;
  60.         }else if(type == 4){
  61.             for(int bit = 0; bit < 32; bit++){
  62.                 if(!(n & (1 << bit))) break;
  63.                 n = n & ~(1 << bit);
  64.             }
  65.             cout << n;
  66.         }else{
  67.             cout << (__builtin_popcount(n) == 1 ? "is power of two" : "not power of two");
  68.         }
  69.         cout << nl;
  70.     }
  71. }
  72.  
  73. int main(){
  74.     //        freopen("equal.in", "r", stdin);
  75.     Start_Crushing();
  76.     int t = 1;
  77. //    /*is Single Test case?*/ cin >> t;
  78.     while (t--) {
  79.         solve();
  80.         if(!t) break;
  81.         cout << "\n";
  82.     }
  83.  
  84.     return 0;
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement