Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 1.57 KB

File metadata and controls

37 lines (27 loc) · 1.57 KB

The BitRef Reference

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 BitVector, BitArray, BitSpan, and friends.

Example

gf2::BitVector v{5};
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");

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 concept API shared by all bit-stores.
  • BitArray for fixed-size vectors of bits.
  • BitVector for dynamically-sized vectors of bits.
  • BitSpan for non-owning views into any bit-store.
  • BitMatrix for matrices of bits.