subset handling in bitsets

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36

#include <iostream>
#include <bitset>
#include <string>

using namespace 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!";
    }
}
Last edited on
Topic archived. No new replies allowed.