I would definitively stick to writing the loop which makes the bit-by-bit assignment. Factor it out as a utility function (not hard at all to write). This is because std::bitset
does NOT fulfill the Container
concept, and thus cannot be sliced or copied using standard algorithms (like std::copy, for example).
Also, notice that because of the implementation you cannot simply get an address to a specific bit (not such a thing, though you can get a "proxy reference").
Just write it (your custom "bit slicer") and forget forever.
Just a sample of how it could look like (it's ugly but it works):
#include<bitset>
#include<iostream>
template<size_t N1, size_t N2>
std::bitset<N1+N2> bitwise_copy(const std::bitset<N1>& b1, const std::bitset<N2>& b2) {
std::bitset<N1+N2> res;
for(size_t k=0; k<N2; k++) {
res[k] = b2[k];
}
for(size_t k=0; k<N1; k++) {
res[N2+k] = b1[k];
}
return std::move(res);
}
int main() {
std::bitset<5> a("11111");
std::bitset<10> b("1010101010");
auto ab = bitwise_copy(a, b);
std::cout<<" a: "<<a<<std::endl
<<" b: "<<b<<std::endl
<<"ab: "<<ab<<std::endl;
return 0;
}
When you compile using gcc 4.8.1 (with -std=c++11
), the output is:
a: 11111
b: 1010101010
ab: 111111010101010