I wanna count the occurences of 1
in multiple bitsets at same position. The count of each position is stored in a vector.
E.g.
b0 = 1011
b1 = 1110
b2 = 0110
----
c = 2231 (1+1+0,0+1+1,1+1+1,1+0+0)
I could do that easily with code below, but this code seems to lack of performance, but I'm not sure. So my question is easily: Is there a faster way to count the 1
?
#include <bitset>
#include <vector>
#include <iostream>
#include <string>
int main(int argc, char ** argv)
{
std::vector<std::bitset<4>> bitsets;
bitsets.push_back(std::bitset<4>("1011"));
bitsets.push_back(std::bitset<4>("1110"));
bitsets.push_back(std::bitset<4>("0110"));
std::vector<unsigned> counts;
for (int i=0,j=4; i<j; ++i)
{
counts.push_back(0);
for (int p=0,q=bitsets.size(); p<q; ++p)
{
if (bitsets[p][(4-1)-i]) // reverse order
{
counts[i] += 1;
}
}
}
for (auto const & count: counts)
{
std::cout << count << " ";
}
}
for (int i=0,j=4; i<j; ++i)
{
for (int p=0,q=b.size(); p<q; ++p)
{
if(b[p][i])
{
c[p] += 1;
}
}
}