fork download
  1. #include <iostream>
  2. #include <limits>
  3. #include <iomanip>
  4.  
  5. template<class T, class = typename std::enable_if<std::is_integral<T>::value>::type, class = typename std::enable_if<!std::is_signed<T>::value>::type>
  6. T revbits(T in)
  7. {
  8. T result = 0;
  9. for (int i = 0; i < std::numeric_limits<T>::digits; i++)
  10. {
  11. result <<= 1;
  12. result |= in & 1;
  13. in >>= 1;
  14. }
  15. return result;
  16. }
  17.  
  18. int main()
  19. {
  20. unsigned short in = 0x1101;
  21. std::cout << std::hex << std::showbase << in << '\n';
  22. std::cout << revbits(in) << '\n';
  23. }
  24.  
Success #stdin #stdout 0s 3340KB
stdin
Standard input is empty
stdout
0x1101
0x8088