升压来,dynamic_bitset - 把一个整数值成一个范围位 [英] Boost dynamic_bitset - put an integer value into a range of bits
问题描述
我有一个7个字节/ 56位的bitset是在建设的第一个位设置一个:
I have a 7-byte/56-bit bitset that upon construction sets the first bit to one:
boost::dynamic_bitset<> b(56, 1);
施工结束后,我想通过15将一个整数值(比如2019)到第4位我很好奇,如果有升压内一个简单的方法来做到这一点没有位操作?基本上,我想一个范围位设置为一个整数值,我知道小到足以放入那些位。感谢您的任何建议。
After construction, I'd like to place an integer value (say 2019) into bits 4 through 15. I'm curious if there is a simple way within boost to do this without bitwise operations? Basically, I want to set a range of bits to an integer value that I know is small enough to fit into those bits. Thanks for any advice.
推荐答案
的的boost ::来,dynamic_bitset&LT;&GT;
提供了非常少的功能。我认为唯一的可能性是使用普通的循环:
The boost::dynamic_bitset<>
offers very less functionality. I think the only possibility is to use a ordinary loop:
template <typename Bitset>
void set_in_range(Bitset& b, unsigned value, int from, int to)
{
for (int i = from; i < to; ++i, value >>= 1)
b[i] = (value & 1);
}
boost::dynamic_bitset<> b(56, 1);
set_in_range(b, 2019, 4, 15);
这篇关于升压来,dynamic_bitset - 把一个整数值成一个范围位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!