/// Stack allocated, so it does not require thread safety checks or disposal.
/// </remarks>
[DebuggerTypeProxy(typeof(BitField32DebugView))]
[GenerateTestsForBurstCompatibility]
publicstructBitField32
{
/// <summary>
/// The 32 bits, stored as a uint.
/// </summary>
/// <value>The 32 bits, stored as a uint.</value>
publicuintValue;
/// <summary>
/// Initializes and returns an instance of BitField32.
/// </summary>
/// <param name="initialValue">Initial value of the bit field. Default is 0.</param>
publicBitField32(uintinitialValue=0u)
{
Value=initialValue;
}
/// <summary>
/// Clears all the bits to 0.
/// </summary>
publicvoidClear()
{
Value=0u;
}
/// <summary>
/// Sets a single bit to 1 or 0.
/// </summary>
/// <param name="pos">Position in this bit field to set (must be 0-31).</param>
/// <param name="value">If true, sets the bit to 1. If false, sets the bit to 0.</param>
/// <exception cref="ArgumentException">Thrown if `pos`is out of range.</exception>
publicvoidSetBits(intpos,boolvalue)
{
CheckArgs(pos,1);
Value=Bitwise.SetBits(Value,pos,1,value);
}
/// <summary>
/// Sets one or more contiguous bits to 1 or 0.
/// </summary>
/// <param name="pos">Position in the bit field of the first bit to set (must be 0-31).</param>
/// <param name="value">If true, sets the bits to 1. If false, sets the bits to 0.</param>
/// <param name="numBits">Number of bits to set (must be 1-32).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 32.</exception>
publicvoidSetBits(intpos,boolvalue,intnumBits)
{
CheckArgs(pos,numBits);
varmask=0xffffffffu>>(32-numBits);
Value=Bitwise.SetBits(Value,pos,mask,value);
}
/// <summary>
/// Returns one or more contiguous bits from the bit field as the lower bits of a uint.
/// </summary>
/// <param name="pos">Position in the bit field of the first bit to get (must be 0-31).</param>
/// <param name="numBits">Number of bits to get (must be 1-32).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 32.</exception>
/// <returns>The requested range of bits from the bit field stored in the least-significant bits of a uint. All other bits of the uint will be 0.</returns>
publicuintGetBits(intpos,intnumBits=1)
{
CheckArgs(pos,numBits);
varmask=0xffffffffu>>(32-numBits);
returnBitwise.ExtractBits(Value,pos,mask);
}
/// <summary>
/// Returns true if the bit at a position is 1.
/// </summary>
/// <param name="pos">Position in the bit field (must be 0-31).</param>
/// <returns>True if the bit at the position is 1.</returns>
publicboolIsSet(intpos)
{
return0!=GetBits(pos);
}
/// <summary>
/// Returns true if none of the bits in a contiguous range are 1.
/// </summary>
/// <param name="pos">Position in the bit field (must be 0-31).</param>
/// <param name="numBits">Number of bits to test (must be 1-32).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 32.</exception>
/// <returns>True if none of the bits in the contiguous range are 1.</returns>
publicboolTestNone(intpos,intnumBits=1)
{
return0u==GetBits(pos,numBits);
}
/// <summary>
/// Returns true if any of the bits in a contiguous range are 1.
/// </summary>
/// <param name="pos">Position in the bit field (must be 0-31).</param>
/// <param name="numBits">Number of bits to test (must be 1-32).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 32.</exception>
/// <returns>True if at least one bit in the contiguous range is 1.</returns>
publicboolTestAny(intpos,intnumBits=1)
{
return0u!=GetBits(pos,numBits);
}
/// <summary>
/// Returns true if all of the bits in a contiguous range are 1.
/// </summary>
/// <param name="pos">Position in the bit field (must be 0-31).</param>
/// <param name="numBits">Number of bits to test (must be 1-32).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 32.</exception>
/// <returns>True if all bits in the contiguous range are 1.</returns>
publicboolTestAll(intpos,intnumBits=1)
{
CheckArgs(pos,numBits);
varmask=0xffffffffu>>(32-numBits);
returnmask==Bitwise.ExtractBits(Value,pos,mask);
}
/// <summary>
/// Returns the number of bits that are 1.
/// </summary>
/// <returns>The number of bits that are 1.</returns>
publicintCountBits()
{
returnmath.countbits(Value);
}
/// <summary>
/// Returns the number of leading zeroes.
/// </summary>
/// <returns>The number of leading zeros.</returns>
publicintCountLeadingZeros()
{
returnmath.lzcnt(Value);
}
/// <summary>
/// Returns the number of trailing zeros.
/// </summary>
/// <returns>The number of trailing zeros.</returns>
thrownewArgumentException($"BitField32 invalid arguments: pos {pos} (must be 0-31), numBits {numBits} (must be 1-32).");
}
}
}
sealedclassBitField32DebugView
{
BitField32BitField;
publicBitField32DebugView(BitField32bitfield)
{
BitField=bitfield;
}
publicbool[]Bits
{
get
{
vararray=newbool[32];
for(inti=0;i<32;++i)
{
array[i]=BitField.IsSet(i);
}
returnarray;
}
}
}
/// <summary>
/// A 64-bit array of bits.
/// </summary>
/// <remarks>
/// Stack allocated, so it does not require thread safety checks or disposal.
/// </remarks>
[DebuggerTypeProxy(typeof(BitField64DebugView))]
[GenerateTestsForBurstCompatibility]
publicstructBitField64
{
/// <summary>
/// The 64 bits, stored as a ulong.
/// </summary>
/// <value>The 64 bits, stored as a uint.</value>
publiculongValue;
/// <summary>
/// Initializes and returns an instance of BitField64.
/// </summary>
/// <param name="initialValue">Initial value of the bit field. Default is 0.</param>
publicBitField64(ulonginitialValue=0ul)
{
Value=initialValue;
}
/// <summary>
/// Clears all bits to 0.
/// </summary>
publicvoidClear()
{
Value=0ul;
}
/// <summary>
/// Sets a single bit to 1 or 0.
/// </summary>
/// <param name="pos">Position in this bit field to set (must be 0-63).</param>
/// <param name="value">If true, sets the bit to 1. If false, sets the bit to 0.</param>
/// <exception cref="ArgumentException">Thrown if `pos`is out of range.</exception>
publicvoidSetBits(intpos,boolvalue)
{
CheckArgs(pos,1);
Value=Bitwise.SetBits(Value,pos,1,value);
}
/// <summary>
/// Sets one or more contiguous bits to 1 or 0.
/// </summary>
/// <param name="pos">Position in the bit field of the first bit to set (must be 0-63).</param>
/// <param name="value">If true, sets the bits to 1. If false, sets the bits to 0.</param>
/// <param name="numBits">Number of bits to set (must be 1-64).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 64.</exception>
publicvoidSetBits(intpos,boolvalue,intnumBits=1)
{
CheckArgs(pos,numBits);
varmask=0xfffffffffffffffful>>(64-numBits);
Value=Bitwise.SetBits(Value,pos,mask,value);
}
/// <summary>
/// Returns one or more contiguous bits from the bit field as the lower bits of a ulong.
/// </summary>
/// <param name="pos">Position in the bit field of the first bit to get (must be 0-63).</param>
/// <param name="numBits">Number of bits to get (must be 1-64).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 64.</exception>
/// <returns>The requested range of bits from the bit field stored in the least-significant bits of a ulong. All other bits of the ulong will be 0.</returns>
publiculongGetBits(intpos,intnumBits=1)
{
CheckArgs(pos,numBits);
varmask=0xfffffffffffffffful>>(64-numBits);
returnBitwise.ExtractBits(Value,pos,mask);
}
/// <summary>
/// Returns true if the bit at a position is 1.
/// </summary>
/// <param name="pos">Position in the bit field (must be 0-63).</param>
/// <returns>True if the bit at the position is 1.</returns>
publicboolIsSet(intpos)
{
return0ul!=GetBits(pos);
}
/// <summary>
/// Returns true if none of the bits in a contiguous range are 1.
/// </summary>
/// <param name="pos">Position in the bit field (must be 0-63).</param>
/// <param name="numBits">Number of bits to test (must be 1-64).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 64.</exception>
/// <returns>True if none of the bits in the contiguous range are 1.</returns>
publicboolTestNone(intpos,intnumBits=1)
{
return0ul==GetBits(pos,numBits);
}
/// <summary>
/// Returns true if any of the bits in a contiguous range are 1.
/// </summary>
/// <param name="pos">Position in the bit field (must be 0-63).</param>
/// <param name="numBits">Number of bits to test (must be 1-64).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 64.</exception>
/// <returns>True if at least one bit in the contiguous range is 1.</returns>
publicboolTestAny(intpos,intnumBits=1)
{
return0ul!=GetBits(pos,numBits);
}
/// <summary>
/// Returns true if all of the bits in a contiguous range are 1.
/// </summary>
/// <param name="pos">Position in the bit field (must be 0-63).</param>
/// <param name="numBits">Number of bits to test (must be 1-64).</param>
/// <exception cref="ArgumentException">Thrown if `pos` or `numBits` are out of bounds or if `pos + numBits` exceeds 64.</exception>
/// <returns>True if all bits in the contiguous range are 1.</returns>
publicboolTestAll(intpos,intnumBits=1)
{
CheckArgs(pos,numBits);
varmask=0xfffffffffffffffful>>(64-numBits);
returnmask==Bitwise.ExtractBits(Value,pos,mask);
}
/// <summary>
/// Returns the number of bits that are 1.
/// </summary>
/// <returns>The number of bits that are 1.</returns>
publicintCountBits()
{
returnmath.countbits(Value);
}
/// <summary>
/// Returns the number of leading zeroes.
/// </summary>
/// <returns>The number of leading zeros.</returns>
publicintCountLeadingZeros()
{
returnmath.lzcnt(Value);
}
/// <summary>
/// Returns the number of trailing zeros.
/// </summary>
/// <returns>The number of trailing zeros.</returns>