fork(1) download
  1. #include <iostream>
  2. #include <bitset>
  3. #include <cstddef>
  4.  
  5.  
  6.  
  7. const size_t CHAR_SIZE = 8;
  8. template <typename T, size_t bitsCount = sizeof(T) * CHAR_SIZE>
  9. T reverse(T original)
  10. {
  11. T result = 0;
  12.  
  13. std::bitset<bitsCount> bN(original);
  14. std::bitset<bitsCount> bR(result);
  15. std::cerr << "Original: " << bN << "\tResult: " << bR << "\n";
  16. for (size_t i = 0; i < bitsCount; ++i)
  17. {
  18. result = result << 1;
  19.  
  20. T rightBit = (original & 0x01);
  21. result = rightBit | result;
  22.  
  23. original = original >> 1;
  24. std::bitset<bitsCount> bN(original);
  25. std::bitset<bitsCount> bR(result);
  26. std::cerr << "Original: " << bN << "\tResult: " << bR << "\n";
  27. }
  28.  
  29. return result;
  30. }
  31.  
  32. int main()
  33. {
  34. reverse(5);
  35. }
Success #stdin #stdout #stderr 0s 3456KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
Original: 00000000000000000000000000000101	Result: 00000000000000000000000000000000
Original: 00000000000000000000000000000010	Result: 00000000000000000000000000000001
Original: 00000000000000000000000000000001	Result: 00000000000000000000000000000010
Original: 00000000000000000000000000000000	Result: 00000000000000000000000000000101
Original: 00000000000000000000000000000000	Result: 00000000000000000000000000001010
Original: 00000000000000000000000000000000	Result: 00000000000000000000000000010100
Original: 00000000000000000000000000000000	Result: 00000000000000000000000000101000
Original: 00000000000000000000000000000000	Result: 00000000000000000000000001010000
Original: 00000000000000000000000000000000	Result: 00000000000000000000000010100000
Original: 00000000000000000000000000000000	Result: 00000000000000000000000101000000
Original: 00000000000000000000000000000000	Result: 00000000000000000000001010000000
Original: 00000000000000000000000000000000	Result: 00000000000000000000010100000000
Original: 00000000000000000000000000000000	Result: 00000000000000000000101000000000
Original: 00000000000000000000000000000000	Result: 00000000000000000001010000000000
Original: 00000000000000000000000000000000	Result: 00000000000000000010100000000000
Original: 00000000000000000000000000000000	Result: 00000000000000000101000000000000
Original: 00000000000000000000000000000000	Result: 00000000000000001010000000000000
Original: 00000000000000000000000000000000	Result: 00000000000000010100000000000000
Original: 00000000000000000000000000000000	Result: 00000000000000101000000000000000
Original: 00000000000000000000000000000000	Result: 00000000000001010000000000000000
Original: 00000000000000000000000000000000	Result: 00000000000010100000000000000000
Original: 00000000000000000000000000000000	Result: 00000000000101000000000000000000
Original: 00000000000000000000000000000000	Result: 00000000001010000000000000000000
Original: 00000000000000000000000000000000	Result: 00000000010100000000000000000000
Original: 00000000000000000000000000000000	Result: 00000000101000000000000000000000
Original: 00000000000000000000000000000000	Result: 00000001010000000000000000000000
Original: 00000000000000000000000000000000	Result: 00000010100000000000000000000000
Original: 00000000000000000000000000000000	Result: 00000101000000000000000000000000
Original: 00000000000000000000000000000000	Result: 00001010000000000000000000000000
Original: 00000000000000000000000000000000	Result: 00010100000000000000000000000000
Original: 00000000000000000000000000000000	Result: 00101000000000000000000000000000
Original: 00000000000000000000000000000000	Result: 01010000000000000000000000000000
Original: 00000000000000000000000000000000	Result: 10100000000000000000000000000000