what\'s the best way to perform bitwise operations on vector?
as i understand, vector is a specialisation that uses
I read both these answers, but just wanted a quick solution, and implemented something horrible.
You can make the bitwise operators work on vector, but the code has to be specialized for the c++ standard library implementation or fall back to the slow form. Here's my operator| for GNU libstdc++-v3:
std::vector operator|(std::vector A, const std::vector& B)
{
if (A.size() != B.size())
throw std::invalid_argument("differently sized bitwise operands");
std::vector::iterator itA = A.begin();
std::vector::const_iterator itB = B.begin();
// c++ implementation-specific
while (itA < A.end())
*(itA._M_p ++) |= *(itB._M_p ++); // word-at-a-time bitwise operation
return A;
}
This is of course pretty bad. Somebody updates GCC, the new version stores things differently, and your code breaks for no apparent reason.