有什么办法可以将两个8位位组分配给一个16位位组 e.g。
bitset<8> first = var1;
bitset<8> second = var2;
bitset<16> = first+second;
答案 0 :(得分:2)
如果您希望序列化它们:
bitset<16> result(first.to_ulong() * 0x100 + second.to_ulong());
首先是最重要的,第二个是最不重要的位。
答案 1 :(得分:2)
将第一位大小移位。
这假设first.size()+ second.size()在c ++ 11中不比ullong大。
bitset<first.size()+second.size()> third(first.to_ullong() << first.size() | second.to_ullong());
答案 2 :(得分:0)
如果您不限制使用bitset&lt;&gt;,您可能会发现逐位运算符最适合此类操作。当然,它更简洁,可能适合也可能不适合您自己的喜好。如,
unsigned bit_1 = 0xff, // low order 8 bits set
bit_2 = 0xff00, // high order 8 bits set
bit_3 = 0,
bit_4 = 0x1ce9;
bit_3 = bit_1 | bit_2; // now bit_3 has all 16 bits set
bit_3 ^= bit_3; // every bit that was set is now cleared
bit_3 = bit_4 & bit_1; // using bit_1 as a mask, bit_3 now contains just the
// lower order bits that are set in bit_4, 0xe9