I’m performing comparisons of objects based on the binary presence or absence of a set of features. These features can be represented by a bit string, such as this:
10011
This bitstring has the first, fourth and fifth feature.
I’m trying to calculate the similarity of a pair of bit strings as the number of features that both share in common. For a given set of bit strings I know that they’ll all have the same length, but I don’t know at compile time what that length will be.
For example, these two strings have two features in common, so I’d like the similarity function to return 2:
s(10011,10010) = 2
How do I efficiently represent and compare bit-strings in C++?
You can use the
std::bitsetSTL class.They can be built from bit strings, ANDed, and count the number of 1:
EDIT
If number of bits is unknown at compile time, you can use
boost::dynamic_bitset<>:Other parts of example don’t change, since
boost::dynamic_bitset<>share a common interface withstd::bitset.