fork download
  1. #include <iostream>
  2. #include <limits>
  3.  
  4. unsigned short ReverseBits(unsigned short input)
  5. {
  6. unsigned short 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 short bits;
  19. Bits(unsigned short value) : bits(value) {}
  20. };
  21.  
  22. std::ostream& operator<<(std::ostream& os, const Bits& b)
  23. {
  24. unsigned short mask = 1 << (std::numeric_limits<unsigned short>::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 short num = 0xDE00;
  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
1101111000000000
0000000001111011