如果位数是编译时常数:
#include <bitset>
...
std::bitset<100> b;
b[2]=true;
如果不是,请使用Boost.dynamic_bitset
或者,如果你很绝望,std::vector,它确实是一个压缩位向量:
#include <vector>
...
std::vector<bool> b(100);
b[2]=true;
您似乎想使用一个需要将位向量打包在字节数组中的库。在不知道它放置位的确切顺序的情况下,我只能注意到:
1) 以上所有内容可能会使用至少 32 位整数,位排序为最低->最高或最高->最低有效
2) 在 little endian (Intel/AMD) CPU 上,这意味着 int 数组的字节占用的内存可能与 int 中的位顺序不一致。如果它是“位 0 是 int 0 的 lsb,... 位 32 是 int 1 的 lsb,...”那么在小端序中这与“位 0 是 char 0 的 lsb,...位32 是 char 4 ..." 的 lsb,在这种情况下,您可以将指向 int 数组的指针转换为指向 char 数组的指针
3) 假设您的位集/向量中的本机字节顺序不是库所需要的,那么您必须要么必须创建自己的具有所需布局的布局,要么将副本转录到他们的布局中.
a) 如果一个字节内的位顺序不同,则一个 256 条目查找表给出位反转的字节将是有效的。你可以用一个小程序生成表格。
b) 从 littlebig endian 反转字节:
inline void endian_swap(unsigned short& x)
{
x = (x>>8) |
(x<<8);
}
inline void endian_swap(unsigned int& x)
{
x = (x>>24) |
((x<<8) & 0x00FF0000) |
((x>>8) & 0x0000FF00) |
(x<<24);
}
inline void endian_swap(unsigned long long& x)
{
x = (x>>56) |
((x<<40) & 0x00FF000000000000) |
((x<<24) & 0x0000FF0000000000) |
((x<<8) & 0x000000FF00000000) |
((x>>8) & 0x00000000FF000000) |
((x>>24) & 0x0000000000FF0000) |
((x>>40) & 0x000000000000FF00) |
(x<<56);
}
要获取/设置一个字中的特定位,位 #0 在字 0 的最低有效位中:
typedef unsigned char block_t;
const unsigned block_bits=8;
inline void set_bit(block_t *d,unsigned i) {
unsigned b=i/block_bits;
unsigned bit=i-(block_bits*b); // same as i%b
block_t &bl=d[b];
bl|=(1<<bit); // or bit with 1 (others anded w/ 0)
}
inline void clear_bit(block_t *d,unsigned i) {
unsigned b=i/block_bits;
unsigned bit=i-(block_bits*b); // same as i%b
block_t &bl=d[b];
bl&=(~(1<<bit)); // and bit with 0 (other bits anded w/ 1)
}
inline void modify_bit(block_t *d,unsigned i,bool val) {
if (val) set_bit(d,i) else clear_bit(d,i);
}
inline bool get_bit(block_t const* d,unsigned i) {
unsigned b=i/block_bits;
unsigned bit=i-(block_bits*b); // same as i%b
return d[b]&(1<<bit);
}
显然,如果位组织规则不同,则必须更改上述内容。
最好使用最广泛的 int CPU 进程,因为 block_t 是最好的(不要忘记更改 block_bits),除非您使用的库无法解决字节序问题。