fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. template <typename T> T f(T x)
  6. {
  7. if (!x || x & 1)
  8. return x;
  9.  
  10. T m = ~(T)0;
  11. for (; m > x; m >>= 1);
  12. return x ^ (m<<1) ^ m;
  13. }
  14.  
  15. int main()
  16. {
  17. for (unsigned x; cin >> x; )
  18. cout << x << ' ' << f(x) << endl;
  19.  
  20. unsigned long long y = -2ULL;
  21. cout << y << ' ' << f(y) << endl;
  22.  
  23. return 0;
  24. }
Success #stdin #stdout 0.01s 5448KB
stdin
0
1
2
3
4
7
8
9
10
14
12345
132456
stdout
0 0
1 1
2 1
3 3
4 1
7 7
8 1
9 9
10 3
14 7
12345 12345
132456 1385
18446744073709551614 9223372036854775807