fork download
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. unsigned int ReverseBits(unsigned int input)
  5. {
  6. unsigned int output = input;
  7. for (int i = sizeof(input) * 8 - 1; i; --i)
  8. {
  9. output <<= 1;
  10. input >>= 1;
  11. output |= input & 1;
  12. }
  13. return output;
  14. }
  15.  
  16. struct Bits
  17. {
  18. unsigned bits;
  19. Bits(unsigned value) : bits(value) {}
  20. };
  21.  
  22. std::ostream& operator<<(std::ostream& os, const Bits& b)
  23. {
  24. unsigned mask = 1 << (std::numeric_limits<unsigned>::digits - 1);
  25.  
  26. while (mask)
  27. {
  28. os << ((b.bits & mask) != 0);
  29. mask >>= 1;
  30. }
  31.  
  32. return os;
  33. }
  34.  
  35. int main()
  36. {
  37. unsigned num = 0xF0F0F0F0;
  38.  
  39. std::cout << Bits(num) << '\n';
  40. std::cout << Bits(ReverseBits(num)) << '\n';
  41. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
11110000111100001111000011110000
00001111000011110000111100001111