Here is my question:
I need to do that very efficiently (I will need to do this operation several billion times on supercomputers) in C
or C++11
. N
and n
are known at compile-time (template parameters). What is the most efficient algorithm to do that ?
Here is an example:
#include <iostream>
#include <climits>
#include <type_traits>
#include <bitset>
template <unsigned int Modulo,
typename Type,
unsigned int Size = sizeof(Type)*CHAR_BIT,
class = typename std::enable_if<std::is_integral<Type>::value
&& std::is_unsigned<Type>::value>::type>
inline Type f(Type x)
{
// The most inefficient algorithm ever
std::bitset<Size> bx(x);
std::bitset<Size> by(0);
unsigned int j = 0;
for (unsigned int i = 0; i < Size; ++i) {
if (i%Modulo) {
by[j++] = bx[i];
}
}
return by.to_ullong();
}
int main()
{
std::bitset<64> x = 823934823;
std::cout<<x<<std::endl;
std::cout<<(std::bitset<64>(f<2>(x.to_ullong())))<<std::endl;
return 0;
}