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
andother
. - 2
-
In-place binary
XOR
between the elements of*this
andother
. - 3
-
In-place binary
OR
between the elements of*this
andother
. - 4
-
In-place binary
XOR
the elements of*this
andother
.
In \(\mathbb{F}_2\), addition corresponds toXOR
. - 5
-
In-place binary
XOR
between the elements of*this
andother
.
In \(\mathbb{F}_2\), subtraction corresponds toXOR
. - 6
-
In-place binary
AND
between the elements of*this
andother
.
In \(\mathbb{F}_2\), multiplication corresponds toAND
.
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()
{
::matrix<> m1(4,[](std::size_t i, std::size_t j) { return (i + j) % 2; });
bitauto 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*