在编译时初始化c ++ std :: bitset [英] Initializing c++ std::bitset at compile time
问题描述
我正在尝试在编译时使用其某些索引来初始化std :: bitset <256>,例如将50-75和200-225设置为1。
I'm trying to initialize a std::bitset<256> at compile time with some of its indices, lets say 50-75 and 200-225 set to 1.
基于 http://en.cppreference.com/w/ cpp / utility / bitset / bitset
看来我的2个选项是:
Based on http://en.cppreference.com/w/cpp/utility/bitset/bitset It looks like my 2 options are:
constexpr bitset();
constexpr bitset( unsigned long long val );
有人考虑第二个构造函数不能很好地解释我如何初始化我的位集吗?
Could someone shed light on how I would initialize my bitset considering the second constructor wouldn't work for large indices?
推荐答案
位集构造函数 constexpr bitset(unsigned long long val)
从某种意义上说,限制是有限的,因为它无法从位置64向上预设位(因为unsigned long long是64位值)。高于64的任何位都将使用 0
进行初始化,如以下示例所示(参见位集构造函数):
Bitset constructor constexpr bitset( unsigned long long val )
is limited in the sense that it cannot pre-set bits from position 64 upwards (as unsigned long long is a 64 bit value). Any bit higher than 64 will be initialized with 0
, as the following example shows (cf. bitset constructors):
std::bitset<70> bl(ULLONG_MAX); // [0,0,0,0,0,0,1,1,1,...,1,1,1] in C++11
如果您还要初始化构造函数中的较高位,我想您就不会使用 string
或 char *
,即类型(3)或(4)的位集构造函数。
If you want to initialize also the higher bits in the constructor, I think you won't get around to use an initializer of type string
or char*
, i.e. bitset constructors of type (3) or (4).
当然,对于长度为256的位集,初始化程序字符串会变长:
Of course, for a bitset of length 256, the initializer string gets long:
std::bitset<256> myBitset("111100000011111000001110000010101000100000100010010000100000000001111");
// I did not count the digits...
这篇关于在编译时初始化c ++ std :: bitset的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!