Fastest way to produce a mask with n ones starting at position i

Fastest way? I’d use something like this:

template <class T>
constexpr T make_mask(std::size_t pos, std::size_t len)
{
  return ((static_cast<T>(1) << len)-1) << pos;
}

Leave a Comment