BitFields
April 18, 2021 ยท View on GitHub
Unmanaged structs for representing large collections of flags and performing bitwise arithmetic on them.
Flag bits are stored in the binary bits of an unmanaged array of UInt32s.
No managed memory is allocated by these structs.
API
Bit Manipulation
Get / Set / Unset / Flip a single bit using the index of that bit.
The operations mutate the data in this struct, usual caveats for mutable value-types apply.
GetBit(int index);
SetBit(int index);
UnsetBit(int index);
FlipBit(int index);
Get / Set / Unset / Flip multiple bits using another bitfield as a mask
The operations mutate the data in this struct, usual caveats for mutable value-types apply.
GetBits(BitField mask);
SetBits(BitField mask);
UnsetBits(BitField mask);
FlipBits(BitField mask);
Bitwise Operators
Static bitwise operators ( & | ^ ~ << >> ) for bitwise arithmetic.
Operators allocate new copies on the stack like any other value type.
Operations are O(n) where n is the number of words that make up the bit field.
Queries
IsEmpty() - Are all the bits set to zero?
HasAllOF(BitField mask) - Are ALL of the flags in the given mask set in the current object?
HasAnyOf(BitField mask) - Are ANY of the flags in the given mask set in the current object?
HasNoneOf(BitField mask) - Are NONE of the flags in the given mask set in the current object?
Indexing and Enumeration
Indexer this[int index] for indexing directly into the bits. (e.g. b[3] = !b[4]).
Set operations mutate the data in this struct, usual caveats for mutable value-types apply.
It is safe to alter the structure while enumerating.
IEquatable
Fast comparison for use in hashtables, overloaded comparison operators ( == != ).