Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 16 additions & 3 deletions BitFaster.Caching.UnitTests/BitOpsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -88,16 +88,29 @@ public void IntBitCount()
BitOps.BitCount(666).Should().Be(5);
}

[Theory]
[InlineData(0U, 0)]
[InlineData(666U, 5)]
[InlineData(uint.MaxValue, 32)]
public void UIntBitCount(uint input, int count)
{
BitOps.BitCount(input).Should().Be(count);
}

[Fact]
public void LongtBitCount()
{
BitOps.BitCount(666L).Should().Be(5);
}

[Fact]
public void ULongtBitCount()

[Theory]
[InlineData(0UL, 0)]
[InlineData(666UL, 5)]
[InlineData(ulong.MaxValue, 64)]
public void ULongtBitCount(ulong input, int count)
{
BitOps.BitCount(666UL).Should().Be(5);
BitOps.BitCount(input).Should().Be(count);
}
}
}
28 changes: 13 additions & 15 deletions BitFaster.Caching/BitOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ public static uint CeilingPowerOfTwo(uint x)
x |= x >> 8;
x |= x >> 16;
return x + 1;
#elif NET6_0_OR_GREATER
return BitOperations.RoundUpToPowerOf2(x);
#else
return 1u << -BitOperations.LeadingZeroCount(x - 1);
#endif
Expand All @@ -65,6 +67,8 @@ internal static ulong CeilingPowerOfTwo(ulong x)
x |= x >> 16;
x |= x >> 32;
return x + 1;
#elif NET6_0_OR_GREATER
return BitOperations.RoundUpToPowerOf2(x);
#else
return 1ul << -BitOperations.LeadingZeroCount(x - 1);
#endif
Expand Down Expand Up @@ -113,14 +117,11 @@ public static int BitCount(int x)
public static int BitCount(uint x)
{
#if NETSTANDARD2_0
var count = 0;
while (x != 0)
{
count++;
x &= x - 1; //walking through all the bits which are set to one
}
x -= (x >> 1) & 0x_55555555u;
x = (x & 0x_33333333u) + ((x >> 2) & 0x_33333333u);
x = (((x + (x >> 4)) & 0x_0F0F0F0Fu) * 0x_01010101u) >> 24;

return count;
return (int)x;
#else
return BitOperations.PopCount(x);
#endif
Expand All @@ -141,20 +142,17 @@ public static int BitCount(long x)
/// </summary>
/// <param name="x">The input parameter.</param>
/// <returns>The number of 1 bits.</returns>
// https://stackoverflow.com/questions/2709430/count-number-of-bits-in-a-64-bit-long-big-integer
public static int BitCount(ulong x)
{
#if NETSTANDARD2_0
var count = 0;
while (x != 0)
{
count++;
x &= x - 1; //walking through all the bits which are set to one
}

return count;
x = x - ((x >> 1) & 0x5555555555555555ul);
x = (x & 0x3333333333333333ul) + ((x >> 2) & 0x3333333333333333ul);
return (int)((((x + (x >> 4)) & 0xF0F0F0F0F0F0F0Ful) * 0x101010101010101ul) >> 56);
#else
return BitOperations.PopCount(x);
#endif

}

/// <summary>
Expand Down
Loading