fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4.  
  5. int main(){
  6. ios::sync_with_stdio(false);
  7. cin.tie(NULL);
  8.  
  9. int n, q;
  10. cin >> n >> q;
  11.  
  12. while(q--)
  13. {
  14. int operation;
  15. cin >> operation;
  16.  
  17. if(operation == 1)
  18. {
  19. for(int i = 0; i < 32; i++)
  20. {
  21. if((1 & (n >> i)) == 0)
  22. {
  23. n |= (1 << i);
  24. break;
  25. }
  26. }
  27. cout << n << '\n';
  28. }
  29. else if(operation == 2)
  30. {
  31. for(int i = 0; i < 32; i++)
  32. {
  33. if((1 & (n >> i)) == 1)
  34. {
  35. n &= (-1 ^ (1 << i));
  36. break;
  37. }
  38. }
  39. cout << n << '\n';
  40. }
  41. else if (operation == 3)
  42. {
  43. if(n == 0)
  44. {
  45. n = -1;
  46. }
  47. else
  48. {
  49. for(int i = 0; i < 32; i++)
  50. {
  51. if((1 & (n >> i)) == 1) // Check first
  52. {
  53. break;
  54. }
  55. n = n | (1 << i); // modify second
  56. }
  57. }
  58. cout << n << '\n'; // output later
  59. }
  60. else if (operation == 4)
  61. {
  62. for(int i = 0; i < 32; i++)
  63. {
  64. if((1 & (n >> i)) == 0) // Check first
  65. {
  66. break;
  67. }
  68. n = n & ~(1 << i);// modify second
  69. }
  70. cout << n << '\n'; // output later
  71. }
  72. else
  73. {
  74. if(__builtin_popcount(n) == 1)
  75. cout << "is power of two\n";
  76. else
  77. cout << "not power of two\n";
  78. }
  79. }
  80.  
  81. return 0;
  82. }
  83.  
Success #stdin #stdout 0.01s 5288KB
stdin
5 7
1
2
3
5
4
1
5
stdout
7
6
7
not power of two
0
1
is power of two