#include using namespace std; //*************************************************** // HackerRank Prepare C++: Variadics / parameter pack //*************************************************** bool g_verbose = true; template void print_all_expander(std::ostream& os, Ts const&... args) { // pack expansion can occur inside a braced initializer list using expander = int[]; (void)expander{0,(void(os << args), 0)...}; } template void print_all_fold(std::ostream& os, Ts const&... args) { ((os << args), ...); } template void print_all_constexpr(std::ostream& os, T const& first, Ts const&... rest) { os << first << ","; if constexpr (sizeof...(rest) > 0) { // this line will only be instantiated if there are further // arguments. if rest... is empty, there will be no call to // print_all_constexpr(os). print_all_constexpr(os, rest...); } } // Enter your code for reversed_binary_value() template int reversed_binary_value() { if (g_verbose) { cout << "reversed_binary_value: print_all: "; print_all_constexpr(cout, digits...); cout << " "; print_all_expander(cout, digits...); cout << " "; print_all_fold(cout, digits...); cout << endl; } bitset bitS; vector bitV = { digits... }; for (size_t i = 0; i < bitV.size(); i++) { if (bitV.at(i)) { bitS.set(i); } } if (g_verbose) cout << "reverse: " << bitS << endl; return (int) bitS.to_ulong(); } template struct CheckValues { static void check(int x, int y) { if (g_verbose) cout << "CheckValues<" << n-1 << ", 0, digits...>" << endl; CheckValues::check(x, y); if (g_verbose) cout << "CheckValues<" << n-1 << ", 1, digits...>" << endl; CheckValues::check(x, y); } }; string res; template struct CheckValues<0, digits...> { static void check(int x, int y) { int z = reversed_binary_value(); //std::cout << (z+64*y==x); res.append(to_string(z+64*y==x)); bitset<64> bs(z); if (g_verbose) { cout << "z: " << bs << " " << bs.to_ulong() << endl; cout << "z+64*y==x: " << res << endl; } } }; void testReverseBinaryVal() { cout << "====== testReverseBinaryVal ========" << endl; cout << "bitset - bitset\n"; constexpr size_t Size = 8; bitset bs("10110000"); bitset reverse; cout << " bitset: " << bs << endl; for (size_t i = 0; i < Size ; i++) if (bs.test(i)) reverse.set(Size - 1 - i); cout << "reverse: " << reverse << " " << reverse.to_ulong() << endl; cout << "-----------------------" << endl; cout << "vector - vector\n"; vector v{1,0,1,1,0,0,0,0}, vrev(Size); for (size_t i = 0; i < v.size(); i++) if (v.at(i)) vrev.at(Size - 1 - i) = true; cout << " v: "; for (auto i: v) {cout << i << " ";} cout << endl; cout << "vrev: "; for (auto i: vrev) {cout << i << " ";} cout << endl; cout << "-----------------------" << endl; cout << "vector - bitset\n"; { bitset bs; for (size_t i = 0; i < v.size(); i++) if (v.at(i)) bs.set(i); cout << "vec to bitset: " << bs << endl; cout << "-----------------------" << endl; } cout << "or and shift left\n"; int result = 0; cout << "input: "; for (auto i: v) cout << (int)i; cout << endl; for (size_t i = 0; i < v.size(); i++) { result |= v.at(i) << i; bitset bs(result); cout << bs << endl; } { bitset bs(result); cout << "result: " << bs << endl; } cout << "-----------------------" << endl; cout << "geeksforgeeks\n"; result = 0; size_t n = 0xB0; // 1101 0000 while (n > 0) { result <<= 1; if ((n & 1) == 1) { result ^= 1; } n >>= 1; } bitset bss(result); cout << "result: " << bss << endl; cout << "-----------------------" << endl; cout << "Shift test\n"; int xxx = 0b1100000011; bitset<10> bsx(xxx); bitset<10> bsy; cout << "x: " << bsx << endl; cout << "y: " << bsy << endl; cout << "****: y = x >> 1\n"; int yyy = xxx >> 1; bsx = xxx; bsy = yyy; cout << "x: " << bsx << endl; cout << "y: " << bsy << endl; cout << "***: x >>= 1\n"; xxx >>= 1; bsx = xxx; cout << "x: " << bsx << endl; } int main() { vector>> testCases = { // x y expected { 65, 1, { (uint64_t)pow(2.0, 64 - 2) } }, { 10, 0, { (uint64_t)pow(2.0, 64 -11) } }, }; //testReverseBinaryVal(); return 0; for (auto test: testCases) { // x is the value to compare against // y represents the range to compare: 64 x y to 64 * y + 63 int x = get<0>(test); int y = get<1>(test); bitset<64> expected(get<2>(test)); res = ""; if (!g_verbose) cout << " actual: "; CheckValues<6>::check(x, y); cout << res; cout << endl; if (!g_verbose) cout << "expected: "; cout << expected << endl; } }