Suppose I am a genome scientist trying to store extremely long strings of characters, each of which represents two bits of information (i.e. each element is either G, A, T, or C). Because the strings are incredibly long, I need to be able to store a string of length N in precisely 2N bits (or rather, N/4 bytes).
With that motivation in mind, I am looking for a generalization of std::bitset (or boost::dynamic_bitset<>) that works on two-bit values instead of single-bit values. I want to store N such two-bit values, each of which can be 0, 1, 2, or 3. I need the data packed as closely as possible in memory, so vector<char> will not work (as it wastes a factor of 4 of memory).
What is the best way to achieve my goal? One option is to wrap the existing bitset templates with customized operator[], iterators, etc., but I'd prefer to use an existing library if at all possible.
std::bitset<2>?!? No way to use less than anuint8_tIMHO. No doubts thatstd::bitsetwill use this as underlying type for decent implementations?chars orstd::vector<char>, do the bit-fiddling yourself, and provide the interface)bitset<2N>work. You could provide an interface that does the necessary index translation.std::array<std::bitset<2>,N>??