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. }
Please click on the like button if it worked

Solution not working or have any suggestions? Please send an email to [email protected]


donate a cup of tea :)


Join Our Facebook Group

Share this solution






codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube