fork(1) download
  1. #include <stdio.h>
  2.  
  3. #define F(T, suf) T f_##suf(T x) \
  4. { \
  5.   if (!x || x & 1) \
  6.   return x; \
  7.   \
  8.   for (unsigned i=0; ; ++i) \
  9.   if ((x>>i) == 1) \
  10.   return x ^ (x>>i<<i) | 1; \
  11. }
  12.  
  13. F(unsigned, u)
  14. F(unsigned long long, ull)
  15.  
  16. int main()
  17. {
  18. unsigned x;
  19.  
  20. while (scanf("%u", &x) == 1)
  21. printf("%u %u\n", x, f_u(x));
  22.  
  23. unsigned long long y = -2LLU;
  24. printf("%llu %llu\n", y, f_ull(y));
  25.  
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 5300KB
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