Table of Contents

Struct Bitmask<T>

Namespace
LfrlAnvil
Assembly
LfrlAnvil.Core.dll

Represents a lightweight generic bitmask.

public readonly struct Bitmask<T> : IEquatable<Bitmask<T>>, IComparable<Bitmask<T>>, IComparable, IReadOnlyCollection<T>, IEnumerable<T>, IEnumerable where T : struct, IConvertible, IComparable

Type Parameters

T

Value type.

Implements
Inherited Members
Extension Methods

Remarks

Type initialization may throw exceptions of BitmaskTypeInitializationException type when the value type is not convertible to and from ulong or when value type is an Enum without the FlagsAttribute or does not have a member with its underlying value equal to 0.

Constructors

Bitmask(T)

Creates a new Bitmask<T> instance.

public Bitmask(T value)

Parameters

value T

Bitmask value.

Fields

All

Represents a bitmask with all available bits set to 1.

public static readonly Bitmask<T> All

Field Value

Bitmask<T>

BaseType

Specifies the base type of the underlying value type.

public static readonly Type BaseType

Field Value

Type

BitCount

Specifies the size of the bitmask in bits.

public static readonly int BitCount

Field Value

int

Empty

Represents an empty bitmask with all bits set to 0.

public static readonly Bitmask<T> Empty

Field Value

Bitmask<T>

FromLongValue

Delegate used for conversion of ulong to the underlying value type.

public static readonly Func<ulong, T> FromLongValue

Field Value

Func<ulong, T>

IsEnum

Specifies whether or not the underlying value type is an Enum.

public static readonly bool IsEnum

Field Value

bool

ToLongValue

Delegate used for conversion of the underlying value type to ulong.

public static readonly Func<T, ulong> ToLongValue

Field Value

Func<T, ulong>

Properties

Count

Specifies the number of set bits in this instance.

public int Count { get; }

Property Value

int

Value

Underlying value.

public T Value { get; }

Property Value

T

Methods

Alternate(Bitmask<T>)

Creates a new Bitmask<T> instance by calculating bitwise xor on this instance and the provided other.

[Pure]
public Bitmask<T> Alternate(Bitmask<T> other)

Parameters

other Bitmask<T>

Value to bitwise xor.

Returns

Bitmask<T>

New Bitmask<T> instance.

Alternate(T)

Creates a new Bitmask<T> instance by calculating bitwise xor on this instance and the provided value.

[Pure]
public Bitmask<T> Alternate(T value)

Parameters

value T

Value to bitwise xor.

Returns

Bitmask<T>

New Bitmask<T> instance.

Clear()

Creates a new Bitmask<T> instance with all bits unset.

[Pure]
public Bitmask<T> Clear()

Returns

Bitmask<T>

New Bitmask<T> instance.

CompareTo(Bitmask<T>)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

[Pure]
public int CompareTo(Bitmask<T> other)

Parameters

other Bitmask<T>

An object to compare with this instance.

Returns

int

A value that indicates the relative order of the objects being compared. The return value has these meanings:

Value Meaning
Less than zero This instance precedes other in the sort order.
Zero This instance occurs in the same position in the sort order as other.
Greater than zero This instance follows other in the sort order.

CompareTo(object?)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

[Pure]
public int CompareTo(object? obj)

Parameters

obj object

An object to compare with this instance.

Returns

int

A value that indicates the relative order of the objects being compared. The return value has these meanings:

Value Meaning
Less than zero This instance precedes obj in the sort order.
Zero This instance occurs in the same position in the sort order as obj.
Greater than zero This instance follows obj in the sort order.

Exceptions

ArgumentException

obj is not the same type as this instance.

ContainsAll(Bitmask<T>)

Checks whether or not the Value of this instance contains all set bits specified by the provided other.

[Pure]
public bool ContainsAll(Bitmask<T> other)

Parameters

other Bitmask<T>

Bits to verify.

Returns

bool

true when this instance contains all set bits specified by the provided other, otherwise false.

ContainsAll(T)

Checks whether or not the Value of this instance contains all set bits specified by the provided value.

[Pure]
public bool ContainsAll(T value)

Parameters

value T

Bits to verify.

Returns

bool

true when this instance contains all set bits specified by the provided value, otherwise false.

ContainsAny(Bitmask<T>)

Checks whether or not the Value of this instance contains at least one set bit specified by the provided other.

[Pure]
public bool ContainsAny(Bitmask<T> other)

Parameters

other Bitmask<T>

Bits to verify.

Returns

bool

true when this instance contains at least one set bit specified by the provided other, otherwise false.

ContainsAny(T)

Checks whether or not the Value of this instance contains at least one set bit specified by the provided value.

[Pure]
public bool ContainsAny(T value)

Parameters

value T

Bits to verify.

Returns

bool

true when this instance contains at least one set bit specified by the provided value, otherwise false.

ContainsBit(int)

Checks whether or not the Value of this instance contains set bit at the specified 0-based bitIndex position.

[Pure]
public bool ContainsBit(int bitIndex)

Parameters

bitIndex int

0-based bit position to verify.

Returns

bool

true when this instance contains set bit at the specified 0-based bitIndex position, otherwise false.

Exceptions

ArgumentOutOfRangeException

When bitIndex is less than 0 or greater than or equal to BitCount.

Equals(Bitmask<T>)

Indicates whether the current object is equal to another object of the same type.

[Pure]
public bool Equals(Bitmask<T> other)

Parameters

other Bitmask<T>

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Equals(object?)

Indicates whether this instance and a specified object are equal.

[Pure]
public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current instance.

Returns

bool

true if obj and this instance are the same type and represent the same value; otherwise, false.

GetEnumerator()

Creates a new Bitmask<T>.Enumerator instance for this bitmask.

[Pure]
public Bitmask<T>.Enumerator GetEnumerator()

Returns

Bitmask<T>.Enumerator

New Bitmask<T>.Enumerator instance.

GetHashCode()

Returns the hash code for this instance.

[Pure]
public override int GetHashCode()

Returns

int

A 32-bit signed integer that is the hash code for this instance.

Intersect(Bitmask<T>)

Creates a new Bitmask<T> instance by calculating bitwise and on this instance and the provided other.

[Pure]
public Bitmask<T> Intersect(Bitmask<T> other)

Parameters

other Bitmask<T>

Value to bitwise and.

Returns

Bitmask<T>

New Bitmask<T> instance.

Intersect(T)

Creates a new Bitmask<T> instance by calculating bitwise and on this instance and the provided value.

[Pure]
public Bitmask<T> Intersect(T value)

Parameters

value T

Value to bitwise and.

Returns

Bitmask<T>

New Bitmask<T> instance.

Negate()

Creates a new Bitmask<T> instance by calculating bitwise not on this instance.

[Pure]
public Bitmask<T> Negate()

Returns

Bitmask<T>

New Bitmask<T> instance.

Sanitize()

Creates a new Bitmask<T> instance by sanitizing this instance by calculating bitwise and with All available values.

[Pure]
public Bitmask<T> Sanitize()

Returns

Bitmask<T>

New Bitmask<T> instance.

Set(Bitmask<T>)

Creates a new Bitmask<T> instance by calculating bitwise or on this instance and the provided other.

[Pure]
public Bitmask<T> Set(Bitmask<T> other)

Parameters

other Bitmask<T>

Value to bitwise or.

Returns

Bitmask<T>

New Bitmask<T> instance.

Set(T)

Creates a new Bitmask<T> instance by calculating bitwise or on this instance and the provided value.

[Pure]
public Bitmask<T> Set(T value)

Parameters

value T

Value to bitwise or.

Returns

Bitmask<T>

New Bitmask<T> instance.

SetBit(int)

Creates a new Bitmask<T> instance by setting a bit at the specified 0-based bitIndex position in the Value of this instance.

[Pure]
public Bitmask<T> SetBit(int bitIndex)

Parameters

bitIndex int

0-based position of a bit to set.

Returns

Bitmask<T>

New Bitmask<T> instance.

Exceptions

ArgumentOutOfRangeException

When bitIndex is less than 0 or greater than or equal to BitCount.

ToString()

Returns a string representation of this Bitmask<T> instance.

[Pure]
public override string ToString()

Returns

string

String representation.

Unset(Bitmask<T>)

Creates a new Bitmask<T> instance by unsetting provided bits in the Value of this instance.

[Pure]
public Bitmask<T> Unset(Bitmask<T> other)

Parameters

other Bitmask<T>

Bits to unset.

Returns

Bitmask<T>

New Bitmask<T> instance.

Unset(T)

Creates a new Bitmask<T> instance by unsetting provided bits in the Value of this instance.

[Pure]
public Bitmask<T> Unset(T value)

Parameters

value T

Bits to unset.

Returns

Bitmask<T>

New Bitmask<T> instance.

UnsetBit(int)

Creates a new Bitmask<T> instance by unsetting a bit at the specified 0-based bitIndex position in the Value of this instance.

[Pure]
public Bitmask<T> UnsetBit(int bitIndex)

Parameters

bitIndex int

0-based position of a bit to unset.

Returns

Bitmask<T>

New Bitmask<T> instance.

Exceptions

ArgumentOutOfRangeException

When bitIndex is less than 0 or greater than or equal to BitCount.

Operators

operator &(Bitmask<T>, Bitmask<T>)

Creates a new Bitmask<T> instance by performing bitwise and on a and b.

[Pure]
public static Bitmask<T> operator &(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator &(Bitmask<T>, T)

Creates a new Bitmask<T> instance by performing bitwise and on a and b.

[Pure]
public static Bitmask<T> operator &(Bitmask<T> a, T b)

Parameters

a Bitmask<T>

First operand.

b T

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator &(T, Bitmask<T>)

Creates a new Bitmask<T> instance by performing bitwise and on a and b.

[Pure]
public static Bitmask<T> operator &(T a, Bitmask<T> b)

Parameters

a T

First operand.

b Bitmask<T>

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator |(Bitmask<T>, Bitmask<T>)

Creates a new Bitmask<T> instance by performing bitwise or on a and b.

[Pure]
public static Bitmask<T> operator |(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator |(Bitmask<T>, T)

Creates a new Bitmask<T> instance by performing bitwise or on a and b.

[Pure]
public static Bitmask<T> operator |(Bitmask<T> a, T b)

Parameters

a Bitmask<T>

First operand.

b T

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator |(T, Bitmask<T>)

Creates a new Bitmask<T> instance by performing bitwise or on a and b.

[Pure]
public static Bitmask<T> operator |(T a, Bitmask<T> b)

Parameters

a T

First operand.

b Bitmask<T>

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator ==(Bitmask<T>, Bitmask<T>)

Checks if a is equal to b.

[Pure]
public static bool operator ==(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

bool

true when operands are equal, otherwise false.

operator ^(Bitmask<T>, Bitmask<T>)

Creates a new Bitmask<T> instance by performing bitwise xor on a and b.

[Pure]
public static Bitmask<T> operator ^(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator ^(Bitmask<T>, T)

Creates a new Bitmask<T> instance by performing bitwise xor on a and b.

[Pure]
public static Bitmask<T> operator ^(Bitmask<T> a, T b)

Parameters

a Bitmask<T>

First operand.

b T

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator ^(T, Bitmask<T>)

Creates a new Bitmask<T> instance by performing bitwise xor on a and b.

[Pure]
public static Bitmask<T> operator ^(T a, Bitmask<T> b)

Parameters

a T

First operand.

b Bitmask<T>

Second operand.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator >(Bitmask<T>, Bitmask<T>)

Checks if a is greater than b.

[Pure]
public static bool operator >(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

bool

true when a is greater than b, otherwise false.

operator >=(Bitmask<T>, Bitmask<T>)

Checks if a is greater than or equal to b.

[Pure]
public static bool operator >=(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

bool

true when a is greater than or equal to b, otherwise false.

implicit operator T(Bitmask<T>)

Converts the provided b to the underlying value type. Returns Value.

[Pure]
public static implicit operator T(Bitmask<T> b)

Parameters

b Bitmask<T>

Bitmask to convert.

Returns

T

Value from the provided b.

implicit operator Bitmask<T>(T)

Converts the provided v to Bitmask<T> type.

[Pure]
public static implicit operator Bitmask<T>(T v)

Parameters

v T

Value to convert.

Returns

Bitmask<T>

New Bitmask<T> instance.

operator !=(Bitmask<T>, Bitmask<T>)

Checks if a is not equal to b.

[Pure]
public static bool operator !=(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

bool

true when operands are not equal, otherwise false.

operator <(Bitmask<T>, Bitmask<T>)

Checks if a is less than b.

[Pure]
public static bool operator <(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

bool

true when a is less than b, otherwise false.

operator <=(Bitmask<T>, Bitmask<T>)

Checks if a is less than or equal to b.

[Pure]
public static bool operator <=(Bitmask<T> a, Bitmask<T> b)

Parameters

a Bitmask<T>

First operand.

b Bitmask<T>

Second operand.

Returns

bool

true when a is less than or equal to b, otherwise false.

operator ~(Bitmask<T>)

Creates a new Bitmask<T> instance by performing bitwise not on a.

[Pure]
public static Bitmask<T> operator ~(Bitmask<T> a)

Parameters

a Bitmask<T>

Operand.

Returns

Bitmask<T>

New Bitmask<T> instance.