GF2++
Loading...
Searching...
No Matches
BitRef

Introduction

A gf2::BitRef is a proxy class that lets you address a single bit in any gf::BitStore object. It behaves like a reference to a boolean value, but under the hood it reads and writes the appropriate bit in the underlying bit-store.

You get a gf2::BitRef from using the non-const version of operator[] on BitVec, BitSet, BitSpan, and friends.

Example

v[0] = true; // writes through BitRef
v[3].flip(); // mutate in-place
bool b = v[0]; // implicit conversion to bool
v[1] ^= b; // compound op stays inside the store
assert_eq(v.to_string(), "10100");
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 BitStore.h:1606
auto flip(usize index)
Flips the value of the bit-element at the given index and returns this for chaining/.
Definition BitStore.h:233
A dynamically-sized vector over GF(2) with bit elements compactly stored in a standard vector of prim...
Definition BitVec.h:37

You can use a BitRef in several ways:

  • Treat it like a bool: it converts implicitly for reads, so you can use it in conditionals or arithmetic on GF(2).
  • Assign to it: v[i] = true, v[i] = other[i], and the compound assignments &=, |=, ^= all work in-place.
  • Flip bits: call bit.flip() or use ^= true to toggle.
Warning
Because a gf2::BitRef keeps a pointer back to the underlying gf2::BitStore, that store must outlive the proxy. Outside of that lifetime guarantee, you can freely pass a BitRef by value – they are tiny and intentionally lightweight.

See Also

  • BitStore for the common API shared by all bit-stores.
  • BitSet for fixed-size vectors of bits.
  • BitVec for dynamically-sized vectors of bits.
  • BitSpan for non-owning views into any bit-store.
  • BitMat for matrices of bits.