HackerRank C++ Variadics



Create a template function named reversed_binary_value. It must take an arbitrary number of bool values as template parameters. These booleans represent binary digits in reverse order. Your function must return an integer corresponding to the binary value of the digits represented by the booleans. For example: reversed_binary_value<0,0,1>() should return 4.



  1. #include <iostream>
  2. using namespace std;
  3. // Enter your code for reversed_binary_value<bool...>()
  4. template<bool... digits>
  5. uint64_t reversed_binary_value()
  6. {
  7. uint64_t pos = 1;
  8. uint64_t result = 0;
  9. auto _ = { (result += digits * pos, pos <<= 1)... };
  10. return result;
  11. }
  12. template <int n, bool...digits>
  13. struct CheckValues {
  14. static void check(int x, int y)
  15. {
  16. CheckValues<n-1, 0, digits...>::check(x, y);
  17. CheckValues<n-1, 1, digits...>::check(x, y);
  18. }
  19. };
  20. template <bool...digits>
  21. struct CheckValues<0, digits...> {
  22. static void check(int x, int y)
  23. {
  24. int z = reversed_binary_value<digits...>();
  25. std::cout << (z+64*y==x);
  26. }
  27. };
  28. int main()
  29. {
  30. int t; std::cin >> t;
  31. for (int i=0; i!=t; ++i) {
  32. int x, y;
  33. cin >> x >> y;
  34. CheckValues<6>::check(x, y);
  35. cout << "\n";
  36. }
  37. }

download android app