mirror of
https://github.com/ahwootech/BRUTAL.git
synced 2026-11-25 20:48:24 +01:00
first
This commit is contained in:
parent
bd62d51ec9
commit
b578433e37
1 changed files with 784842 additions and 0 deletions
27
Brutal.Core/Extensions/BitOperationsEx.cs
Normal file
27
Brutal.Core/Extensions/BitOperationsEx.cs
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
namespace System.Numerics;
|
||||
|
||||
public static class BitOperationsEx
|
||||
{
|
||||
extension(BitOperations)
|
||||
{
|
||||
/// <summary>
|
||||
/// If <paramref name="mask"/> has only a single bit set, output its <paramref name="index"/>, otherwise return false.
|
||||
/// </summary>
|
||||
/// <param name="mask">A bit-mask value that potentially only contains a single bit.</param>
|
||||
/// <param name="index">The index of the single bit, if true.</param>
|
||||
/// <returns>Whether <paramref name="mask"/> contains a single bit.</returns>
|
||||
public static bool TryGetBitIndex<T>(T mask, out byte index)
|
||||
where T : unmanaged, IBinaryInteger<T>
|
||||
{
|
||||
if (!T.IsPow2(mask))
|
||||
{
|
||||
index = 0;
|
||||
return false;
|
||||
}
|
||||
|
||||
var tzc = T.TrailingZeroCount(mask);
|
||||
index = byte.CreateTruncating(tzc);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue