我需要从给定的boost dynamic_bitset中提取和解码位(idx,idx + 1,... idx + n_bits).我创建了以下解决方案:
boost::dynamic_bitset<> mybitset(...);
// build mask 2^{idx+n_bits} - 2^{idx}
const boost::dynamic_bitset<> mask(mybitset.size(), (1 << idx+n_bits) - (1 << idx));
// shift the masked result idx times and get long
unsigned long u = ((mybitset & mask) >> idx ).to_ulong();
Run Code Online (Sandbox Code Playgroud)
它运行良好,但由于此代码对我的应用程序的性能至关重要,我很好奇是否有更好的方法来实现这一点?