Sort by

recency

|

130 Discussions

|

  • + 0 comments
    template <bool...digits>
    int reversed_binary_value(){
        bool arr[] = {digits...};
        int sum = 0;
        int a = 0;
        for(bool i : arr){
            sum = sum | ((1&i) << a++);
        }
        return sum;
    }
    
  • + 0 comments

    This is a stupid example, doesn't make any sense

  • + 0 comments
    template <bool... digits> 
    int reversed_binary_value(){
        std::vector<int> parameterPack = {digits...};
        //std::reverse(parameterPack.begin(), parameterPack.end());
        int a = 0;
        int sum = 0;
        for (bool i : parameterPack)
        {
            sum = sum + std::pow(2,a)*i;
            a++;
        }
        return sum;
    }
    
  • + 0 comments
    template <bool a> int reversed_binary_value() { return a; }
    
    template <bool a, bool b, bool... d> int reversed_binary_value() {
      return (reversed_binary_value<b, d...>() << 1) + a;
    }
    
  • + 0 comments

    Decided to play with MetaProgramming,

    template struct constexpr_pow { constexpr static uint64_t value() { return base * constexpr_pow::value(); } };

    template struct constexpr_pow { constexpr static uint64_t value() { return 1; } }; static_assert(constexpr_pow<2, 0>::value() == 1, "Failed pow(2,0)"); static_assert(constexpr_pow<2, 1>::value() == 2, "Failed pow(2,1)"); template constexpr uint64_t reversed_binary_value_impl() { return 0; } // 0, 0, [1...] template constexpr uint64_t reversed_binary_value_impl() { return digit * constexpr_pow<2, n>::value() + reversed_binary_value_impl(); } template constexpr uint64_t reversed_binary_value() { return reversed_binary_value_impl<0, digits...>(); } static_assert(reversed_binary_value<0>() == 0, "Failed 0b0"); static_assert(reversed_binary_value<1>() == 1, "Failed 0b1");