I have a vector
of bool
which I want to copy in a int
container of bigger size. Is there a fast way to do this?
To clarify, is there a smarter way to achieve this?
#include <vector>
#include <cstdint>
#include <iostream>
#include <climits>
#include <cassert>
inline size_t bool2size_t(std::vector<bool> in) {
assert(sizeof(size_t)*CHAR_BIT >= in.size());
size_t out(0);
for (size_t vecPos = 0; vecPos < in.size(); vecPos++) {
if (in[vecPos]) {
out += 1 << vecPos;
}
}
return out;
}
int main () {
std::vector<bool> A(10,0);
A[2] = A[4] = 1;
size_t B = bool2size_t(A);
std::cout << (1 << 2) + (1 << 4) << std::endl;
std::cout << B << std::endl;
}
I'm looking for something like a memcpy
which I can use on a subbyte level.