fork download
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. unsigned inv(unsigned x)
  6. {
  7. unsigned y = x;
  8.  
  9. y |= y >> 1;
  10. y |= y >> 2;
  11. y |= y >> 4;
  12. y |= y >> 8;
  13. y |= y >> 16;
  14.  
  15. return x ^ y;
  16. }
  17.  
  18. int main()
  19. {
  20. int a[] = { 0, 1, 2, 3, 4, 5, 6, 7, 255, 256, 317, 402, 7200, 86123456, 1000000000, 1234567897 };
  21.  
  22. for (unsigned q=0; q < sizeof a / sizeof *a; ++q)
  23. cout << a[q] << ' ' << inv(a[q]) << endl;
  24.  
  25. return 0;
  26. }
Success #stdin #stdout 0s 15232KB
stdin
Standard input is empty
stdout
0 0
1 0
2 1
3 0
4 3
5 2
6 1
7 0
255 0
256 255
317 194
402 109
7200 991
86123456 48094271
1000000000 73741823
1234567897 912915750