My program deals with lots of bitsets that includes comparing subset of one bitset with another subset of same or other bitset. For example:
std::bitset<100> A and std::bitset<100> B are bitsets and I would like to 1) extract the subset from 10 to 90 and store it in another bitset 2) Compare the bits from 10 to 40 in A with bits from 30 - 60 in B. The brute force approach is to scanning through all bits and creating another bitset. Is there any faster and efficient approach?
Though iterators won't work with bitsets you can compare the subsets in situ, i.e without extracting them, using standard comparison techniques. Depending on the match results you can then decide to extract if required:
#include <iostream>
#include <bitset>
#include <string>
usingnamespace std;
int main()
{
bitset<12> A {"010101010101"};
bitset<12> B {"000101010001"};
size_t range = 3;
bool match = true;
size_t i = 0;
while (i < range)//with an outer loop you can also check 'moving' sub-ranges;
{
if(A[3+i] == B[3+i] )
{
i++;
}
else
{
cout<<"No match"<<"\n";
match = false;
break;
}
}
if (match == true)
{
cout<<"Match!";
}
}