in bitset, can i use “to_ulong” for a specific range of bits?

佐手、 提交于 2019-11-27 06:41:02

问题


hi im working on something that demands me to get access to specific/range of bits. i decided to use bitset because it is easy to get access to specific bits but can i extract a whole range of bits?


回答1:


Method A:

return (the_bitset >> start_bit).to_ulong();

Method B (faster than method A by 100 times on my machine):

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;


来源:https://stackoverflow.com/questions/2177186/in-bitset-can-i-use-to-ulong-for-a-specific-range-of-bits

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!