Extension methods for reading and manipulating the individual bits of a number type T.
public bool IsBitSet(int idx);Returns true if the bit at position idx is 1.
ℹ️ idx is zero-based, where 0 is the least significant bit.
Throws ArgumentOutOfRangeException if idx is negative or exceeds the bit width of T.
public T ReplaceBit(int idx, bool value);Returns a copy of the number with the bit at position idx set according to value.
public T SetBit(int idx);Returns a copy of the number with the bit at position idx set to 1.
public T UnsetBit(int idx);Returns a copy of the number with the bit at position idx set to 0.
public static T FromBits(params bool[] bits);Constructs a number T from an array of booleans ordered from least to most significant bit.
Reading and modifying bits.
byte val = 0b_0000_1010;
val.IsBitSet(0); // false (bit 0 is not set)
val.IsBitSet(1); // true (bit 1 is set)
val = val.SetBit(0).UnsetBit(1);
// 0b_0000_1001Constructing a number from individual bits.
var val = byte.FromBits(true, false, true, false, true, true);
// 0b_0011_0101