Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在做一些需要我访问特定位和位范围的事情。我决定使用 bitset,因为它很容易访问特定的位;如何提取位范围(子集)?
方法一:
return (the_bitset >> start_bit).to_ulong();
方法 B(在我的机器上比方法 A 快 100 倍):
unsigned long mask = 1; unsigned long result = 0; for (size_t i = start_bit; i < end_bit; ++ i) { if (the_bitset.test(i)) result |= mask; mask <<= 1; } return result;