fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void print(int n)
  5. {
  6. int mask = 1<<30, cnt = 31;
  7. cout<<mask<<" "<<n<<endl;
  8. while(cnt--)
  9. {
  10. if(n & mask)
  11. cout<<"1 ";
  12. else
  13. cout<<"0 ";
  14. mask = mask>>1;
  15. }
  16. cout<<endl;
  17. }
  18.  
  19. void flipSecMSB(int n)
  20. {
  21. print(n);
  22.  
  23. int mask = 1<<30, cnt = 31; bool foundMSB=false;
  24. while(cnt--)
  25. {
  26. if(n & mask)
  27. {
  28. if(foundMSB)
  29. {
  30. n = n ^ mask;
  31. break;
  32. }
  33. else
  34. foundMSB = true;
  35. }
  36. mask = mask>>1;
  37. }
  38. print(n);
  39. }
  40.  
  41. int main() {
  42. // your code goes here
  43. flipSecMSB(8);
  44. flipSecMSB(255);
  45.  
  46. return 0;
  47. }
Success #stdin #stdout 0s 3140KB
stdin
Standard input is empty
stdout
1073741824 8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 
1073741824 8
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 
1073741824 255
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 
1073741824 191
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1