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;
}
More Related Contents:
- Position of least significant bit that is set
- Branchless code that maps zero, negative, and positive to 0, 1, 2
- Best practices for circular shift (rotate) operations in C++
- Measuring execution time of a function in C++
- Which, if any, C++ compilers do tail-recursion optimization?
- How to overload std::swap()
- Why is transposing a matrix of 512×512 much slower than transposing a matrix of 513×513?
- How to write a large buffer into a binary file in C++, fast?
- Is it better to use std::memcpy() or std::copy() in terms to performance?
- Rounding up to next power of 2
- C/C++: Force Bit Field Order and Alignment
- Why does optimisation kill this function?
- What is the idea behind ^= 32, that converts lowercase letters to upper and vice versa?
- How to get IOStream to perform better?
- Why should I ever use inline code?
- get absolute value without using abs function nor if statement
- Does a c/c++ compiler optimize constant divisions by power-of-two value into shifts?
- What happens when you bit shift beyond the end of a variable?
- How to use profile guided optimizations in g++?
- What’s the fastest way to pack 32 0/1 values into the bits of a single 32-bit variable?
- C++ error: ‘_mm_sin_ps’ was not declared in this scope
- Fast multiplication/division by 2 for floats and doubles (C/C++)
- Loop with a zero execution time
- std::vector reserve() and push_back() is faster than resize() and array index, why?
- Is it legal for a C++ optimizer to reorder calls to clock()?
- Optimizing member variable order in C++
- Is Visual Studio 2013 optimizing correctly in the presence of /OPT:ICF?
- How can adding code to a loop make it faster?
- How to reduce the size of executable produced by MinGW g++ compiler?
- std::array with aggregate initialization on g++ generates huge code