GF2++
Loading...
Searching...
No Matches
gf2::BitRef< Store >

A BitRef is a proxy class to reference a single bit in a bit-store. More...

#include <BitRef.h>

Public Member Functions

 BitRef (Store *store, usize index)
 Constructs a reference to the bit at index in the given bit-store.
constexpr operator bool () const
 Returns the boolean value of the bit at index in the referenced bit-store.
constexpr BitRefoperator= (bool rhs)
 Sets the bit at index in the referenced bit-store to the given value.
constexpr BitRefoperator= (BitRef const &rhs)
 Sets the bit at index in the referenced bit-store to the value of the given BitRef.
constexpr BitRefflip ()
 Flips the bit at index in the referenced bit-store.
constexpr BitRefoperator&= (bool rhs)
 Performs an AND operation on the bit at index in the referenced bit-store with rhs.
constexpr BitRefoperator&= (BitRef const &rhs)
 Performs an AND operation on the bit at index in the referenced bit-store with rhs.
constexpr BitRefoperator|= (bool rhs)
 Performs an OR operation on the bit at index in the referenced bit-store with rhs.
constexpr BitRefoperator|= (BitRef const &rhs)
 Performs an OR operation on the bit at index in the referenced bit-store with rhs.
constexpr BitRefoperator^= (bool rhs)
 Performs an XOR operation on the bit at index in the referenced bit-store with rhs.
constexpr BitRefoperator^= (BitRef const &rhs)
 Performs an XOR operation on the bit at index in the referenced bit-store with rhs.

Detailed Description

template<BitStore Store>
class gf2::BitRef< Store >

A BitRef is a proxy class to reference a single bit in a bit-store.

If v is any non-const bit-store then v[i] is a BitRef that "references" the bit at index i in v. It behaves as a proxy for that element and allows us to set or get the bit value via the = operator.

The end user can also treat the BitRef object as a reference to a boolean value and use it in boolean expressions.

Note
The underlying bit-store must live as long as the BitRef that refers to it.

Example

BitVec v{3};
assert_eq(v.to_string(), "000");
v[1] = true;
assert_eq(v.to_string(), "010");
v[2] = 1;
assert_eq(v.to_string(), "011");
A dynamically-sized vector over GF(2) with bit elements compactly stored in a standard vector of prim...
Definition BitVec.h:23
std::string to_string(std::string_view sep="", std::string_view pre="", std::string_view post="") const
Returns a binary string representation of the store.
Definition BitVec.h:1650

Member Function Documentation

◆ flip()

template<BitStore Store>
BitRef & gf2::BitRef< Store >::flip ( )
inlineconstexpr

Flips the bit at index in the referenced bit-store.

Example

auto v = BitVec<>::zeros(3);
ref(v, 0).flip();
assert_eq(to_string(v), "100");
static constexpr BitVec zeros(usize n)
Factory method to generate a bit-vector of length n where the elements are all 0.
Definition BitVec.h:194
static std::string to_string(Store const &store, std::string_view sep="", std::string_view pre="", std::string_view post="")
Returns a binary string representation of a store.
Definition BitStore.h:1482
constexpr auto ref(Store &store, usize i)
Returns a "reference" to the bit element i in the given bit-store.
Definition BitStore.h:189

◆ operator bool()

template<BitStore Store>
gf2::BitRef< Store >::operator bool ( ) const
inlineconstexpr

Returns the boolean value of the bit at index in the referenced bit-store.

Example

BitVec v{3};
auto bit = ref(v, 0);
assert_eq(static_cast<bool>(bit), false);

◆ operator&=() [1/2]

template<BitStore Store>
BitRef & gf2::BitRef< Store >::operator&= ( BitRef< Store > const & rhs)
inlineconstexpr

Performs an AND operation on the bit at index in the referenced bit-store with rhs.

Example

auto v = BitVec<>::ones(3);
auto w = BitVec<>::zeros(3);
ref(v, 0) &= ref(w, 0);
assert_eq(to_string(v), "011");
static constexpr BitVec ones(usize n)
Factory method to generate a bit-vector of length n where the elements are all 1.
Definition BitVec.h:202

◆ operator&=() [2/2]

template<BitStore Store>
BitRef & gf2::BitRef< Store >::operator&= ( bool rhs)
inlineconstexpr

Performs an AND operation on the bit at index in the referenced bit-store with rhs.

Example

auto v = BitVec<>::ones(3);
ref(v, 0) &= false;
assert_eq(to_string(v), "011");

◆ operator=() [1/2]

template<BitStore Store>
BitRef & gf2::BitRef< Store >::operator= ( BitRef< Store > const & rhs)
inlineconstexpr

Sets the bit at index in the referenced bit-store to the value of the given BitRef.

Example

auto v = BitVec<>::zeros(3);
auto w = BitVec<>::ones(3);
ref(v, 0) = ref(w, 0);
assert_eq(to_string(v), "100");
ref(v, 1) = ref(w, 1);
assert_eq(to_string(v), "110");
ref(v, 2) = ref(w, 2);
assert_eq(to_string(v), "111");

◆ operator=() [2/2]

template<BitStore Store>
BitRef & gf2::BitRef< Store >::operator= ( bool rhs)
inlineconstexpr

Sets the bit at index in the referenced bit-store to the given value.

Example

BitVec v{3};
ref(v, 0) = true;
assert_eq(to_string(v), "100");

◆ operator^=() [1/2]

template<BitStore Store>
BitRef & gf2::BitRef< Store >::operator^= ( BitRef< Store > const & rhs)
inlineconstexpr

Performs an XOR operation on the bit at index in the referenced bit-store with rhs.

Example

auto v = BitVec<>::ones(3);
auto w = BitVec<>::ones(3);
ref(v, 0) ^= ref(w, 0);
assert_eq(to_string(v), "011");

◆ operator^=() [2/2]

template<BitStore Store>
BitRef & gf2::BitRef< Store >::operator^= ( bool rhs)
inlineconstexpr

Performs an XOR operation on the bit at index in the referenced bit-store with rhs.

Example

auto v = BitVec<>::ones(3);
ref(v, 0) ^= true;
assert_eq(to_string(v), "011");

◆ operator|=() [1/2]

template<BitStore Store>
BitRef & gf2::BitRef< Store >::operator|= ( BitRef< Store > const & rhs)
inlineconstexpr

Performs an OR operation on the bit at index in the referenced bit-store with rhs.

Example

auto v = BitVec<>::zeros(3);
auto w = BitVec<>::ones(3);
ref(v, 0) |= ref(w, 0);
assert_eq(to_string(v), "100");

◆ operator|=() [2/2]

template<BitStore Store>
BitRef & gf2::BitRef< Store >::operator|= ( bool rhs)
inlineconstexpr

Performs an OR operation on the bit at index in the referenced bit-store with rhs.

Example

auto v = BitVec<>::zeros(3);
ref(v, 0) |= true;
assert_eq(to_string(v), "100");