namespace System.Numerics;
public static class BitOperationsEx
{
extension(BitOperations)
{
///
/// If has only a single bit set, output its , otherwise return false.
///
/// A bit-mask value that potentially only contains a single bit.
/// The index of the single bit, if true.
/// Whether contains a single bit.
public static bool TryGetBitIndex(T mask, out byte index)
where T : unmanaged, IBinaryInteger
{
if (!T.IsPow2(mask))
{
index = 0;
return false;
}
var tzc = T.TrailingZeroCount(mask);
index = byte.CreateTruncating(tzc);
return true;
}
}
}