GF2::Vector — Dot Product for Bit-Vectors

Computes the dot product of two equal sized bit-vectors.

template<std::unsigned_integral Block, typename Allocator>
constexpr bool
dot(const Vector<Block, Allocator> &u,
    const Vector<Block, Allocator> &v);

The dot product of two vectors is defined by the equation

\[\mathbf{u} \cdot \mathbf{v} = \sum_i u_i \times v_i.\]

In the case of bit-vectors, products are replaced by logical AND, and sums by the logical XOR operation.

The dot product is a key operation in linear algebra so it fortunate that AND’ing and XOR’ing for bit-vectors can be done very efficiently over blocks of elements at a time. The required result is just the one-liner (lhs & rhs).parity().

The two vectors in question must be of the same size. If the GF2_DEBUG flag is set at compile time this is checked and any violation will cause the program to abort with a helpful message.
Example
#include <GF2/GF2.h>
int main()
{
    GF2::Vector<> u("01111110");
    GF2::Vector<> v("10101010");
    std::cout << u << " dot " << v << " yields " << GF2::dot(u, v) << '\n';
}
Output
01111110 dot 10101010 yields 1