diff --git a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/BitVector32.cs b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/BitVector32.cs
index 12853ebe3622d489f1b6c73f2282753d24bb97e4..ce4ba761ab7affe2b01f675aca1fbb91750176fa 100644
--- a/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/BitVector32.cs
+++ b/src/libraries/System.Collections.Specialized/src/System/Collections/Specialized/BitVector32.cs
@@ -1,9 +1,8 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
-using System.Diagnostics;
-using System.Text;
using System.Diagnostics.CodeAnalysis;
+using System.Numerics;
namespace System.Collections.Specialized
{
@@ -93,18 +92,6 @@ public int Data
}
}
- private static short CountBitsSet(short mask)
- {
- // We assume that the bits are always right aligned, with no holes (i.e. always 00000111, never 00100011)
- short value = 0;
- while ((mask & 0x1) != 0)
- {
- value++;
- mask >>= 1;
- }
- return value;
- }
-
///
/// Creates the first mask in a series.
///
@@ -131,29 +118,6 @@ public static int CreateMask(int previous)
return previous << 1;
}
- ///
- /// Given a highValue, creates the mask
- ///
- private static short CreateMaskFromHighValue(short highValue)
- {
- short required = 16;
- while ((highValue & 0x8000) == 0)
- {
- required--;
- highValue <<= 1;
- }
-
- ushort value = 0;
- while (required > 0)
- {
- required--;
- value <<= 1;
- value |= 0x1;
- }
-
- return unchecked((short)value);
- }
-
///
/// Creates the first section in a series, with the specified maximum value.
///
@@ -177,28 +141,19 @@ private static Section CreateSectionHelper(short maxValue, short priorMask, shor
throw new ArgumentException(SR.Format(SR.Argument_InvalidValue_TooSmall, nameof(maxValue), 1), nameof(maxValue));
}
- short offset = (short)(priorOffset + CountBitsSet(priorMask));
+ short offset = (short)(priorOffset + BitOperations.PopCount((uint)(ushort)priorMask));
if (offset >= 32)
{
throw new InvalidOperationException(SR.BitVectorFull);
}
- return new Section(CreateMaskFromHighValue(maxValue), offset);
- }
-
- public override bool Equals([NotNullWhen(true)] object? o)
- {
- if (!(o is BitVector32))
- {
- return false;
- }
- return _data == ((BitVector32)o)._data;
+ short mask = (short)(BitOperations.RoundUpToPowerOf2((uint)(ushort)maxValue + 1) - 1);
+ return new Section(mask, offset);
}
- public override int GetHashCode()
- {
- return base.GetHashCode();
- }
+ public override bool Equals([NotNullWhen(true)] object? o) => o is BitVector32 other && _data == other._data;
+
+ public override int GetHashCode() => _data.GetHashCode();
public static string ToString(BitVector32 value)
{
@@ -238,29 +193,11 @@ internal Section(short mask, short offset)
_offset = offset;
}
- public short Mask
- {
- get
- {
- return _mask;
- }
- }
+ public short Mask => _mask;
- public short Offset
- {
- get
- {
- return _offset;
- }
- }
+ public short Offset => _offset;
- public override bool Equals([NotNullWhen(true)] object? o)
- {
- if (o is Section)
- return Equals((Section)o);
- else
- return false;
- }
+ public override bool Equals([NotNullWhen(true)] object? o) => o is Section other && Equals(other);
public bool Equals(Section obj)
{
@@ -277,14 +214,11 @@ public bool Equals(Section obj)
return !(a == b);
}
- public override int GetHashCode()
- {
- return base.GetHashCode();
- }
+ public override int GetHashCode() => HashCode.Combine(_mask, _offset);
public static string ToString(Section value)
{
- return "Section{0x" + Convert.ToString(value.Mask, 16) + ", 0x" + Convert.ToString(value.Offset, 16) + "}";
+ return $"Section{{0x{value.Mask:x}, 0x{value.Offset:x}}}";
}
public override string ToString()