bit::matrix — In-place Logical Operators

Methods to perform element-by-element binary AND, XOR, OR, +, -, * with another equal-sized bit-matrix.

1constexpr bit::matrix &operator&=(const bit::matrix &other);
2constexpr bit::matrix &operator^=(const bit::matrix &other);
3constexpr bit::matrix &operator|=(const bit::matrix &other);
4constexpr bit::matrix &operator+=(const bit::matrix &other);
5constexpr bit::matrix &operator-=(const bit::matrix &other);
6constexpr bit::matrix &operator*=(const bit::matrix &other);
1
In-place binary AND between the elements of *this and other.
2
In-place binary XOR between the elements of *this and other.
3
In-place binary OR between the elements of *this and other.
4
In-place binary XOR the elements of *this and other.
In \(\mathbb{F}_2\), addition corresponds to XOR.
5
In-place binary XOR between the elements of *this and other.
In \(\mathbb{F}_2\), subtraction corresponds to XOR.
6
In-place binary AND between the elements of *this and other.
In \(\mathbb{F}_2\), multiplication corresponds to AND.

These methods all return a reference to *this so they can be chained with other calls.

The two bit-matrices in question must have the same dimensions. Set the BIT_VERIFY flag at compile time to check this condition — any violation will cause the program to abort with a helpful message.

There is one other bit-twiddling method:

1constexpr bit::matrix operator~() const;
1
Returns a copy of the bit-matrix with all the bits flipped

Example

#include <bit/bit.h>
int main()
{
    bit::matrix<> m1(4,[](std::size_t i, std::size_t j) { return (i + j) % 2; });
    auto m2 = bit::matrix<>::ones(4);

    std::cout << "m1:\n" << m1  << '\n';
    std::cout << "m2:\n" << m2  << '\n';
    std::cout << "m1 &= m2:\n"  << (m1 &= m2)   << '\n';
    std::cout << "m1 |= m2:\n"  << (m1 |= m2)   << '\n';
    std::cout << "m1 ^= m2:\n"  << (m1 ^= m2)   << '\n';
    std::cout << "~m1:\n"       << (~m1)        << '\n';
}

Output

m1:
│0 1 0 1│
│1 0 1 0│
│0 1 0 1│
│1 0 1 0│
m2:
│1 1 1 1│
│1 1 1 1│
│1 1 1 1│
│1 1 1 1│
m1 &= m2:
│0 1 0 1│
│1 0 1 0│
│0 1 0 1│
│1 0 1 0│
m1 |= m2:
│1 1 1 1│
│1 1 1 1│
│1 1 1 1│
│1 1 1 1│
m1 ^= m2:
│0 0 0 0│
│0 0 0 0│
│0 0 0 0│
│0 0 0 0│
~m1:
│1 1 1 1│
│1 1 1 1│
│1 1 1 1│
│1 1 1 1│

See Also

matrix::operator&
matrix::operator|
matrix::operator^
matrix::operator+
matrix::operator-
matrix::operator*

Back to top