The common implementations usually reference: https://github.com/fmatthew5876/stdcxx-bitops/blob/master/include/bitops.hh
I styled my version to match (except for the constexpr macro):
template <typename Integral> constexpr Integral extract_bits(Integral x, Integral mask) { Integral res=0; int bb=1; do { Integral lsb=mask & -mask; mask &= ~lsb; bool isset=x & lsb; res |= isset ? bb : 0; bb+=bb; } while (mask); return res; } template <typename Integral> constexpr Integral deposit_bits(Integral x, Integral mask) { Integral res = 0; int bb=1; do { Integral lsb=mask & -mask; mask &= ~lsb; bool isset=x&bb; res |= (x & bb) ? lsb : 0; bb+=bb; } while(mask); return res; }
for-loop in ado-whileloop, and theifinto a ternary? I'm not really sure this is suitable for Code Review. \$\endgroup\$constexprkeyword" there? \$\endgroup\$