From 9a77ddd5017c72c0cf60ae9c0cf9327141fcd7ce Mon Sep 17 00:00:00 2001 From: Vladimir Pecanac Date: Sun, 8 Nov 2026 14:47:30 +0100 Subject: [PATCH] BitArray in CSharp: move to .NET 10, add examples and an And benchmark Target net10.0 and update BenchmarkDotNet, xUnit, the test SDK and coverlet. Add Examples.cs with the snippets the article shows, run them by default and the benchmarks with --bench, add BitArrayAndBenchmark, move to file-scoped namespaces and add six tests for bit order, shifts, HasAnySet/HasAllSet, CopyTo, counting set bits and And length mismatch. --- .../BitArray in CSharp.csproj | 4 +- .../BitArray in CSharp/BitArrayBenchmark.cs | 75 +++++++--- .../BitArrayExtensionMethods.cs | 15 +- .../BitArray in CSharp/Examples.cs | 130 ++++++++++++++++++ .../BitArray in CSharp/Program.cs | 9 +- .../Tests/BitArrayInCSharpUnitTests.cs | 58 +++++++- .../BitArray in CSharp/Tests/Tests.csproj | 14 +- 7 files changed, 269 insertions(+), 36 deletions(-) create mode 100644 collections-csharp/BitArray in CSharp/BitArray in CSharp/Examples.cs diff --git a/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArray in CSharp.csproj b/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArray in CSharp.csproj index 3cb11cece6..f4537df3af 100644 --- a/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArray in CSharp.csproj +++ b/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArray in CSharp.csproj @@ -2,14 +2,14 @@ Exe - net7.0 + net10.0 BitArray_in_CSharp enable enable - + diff --git a/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArrayBenchmark.cs b/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArrayBenchmark.cs index f683bcc03e..bee04f4370 100644 --- a/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArrayBenchmark.cs +++ b/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArrayBenchmark.cs @@ -1,32 +1,69 @@ using BenchmarkDotNet.Attributes; using System.Collections; -namespace BitArray_in_CSharp +namespace BitArray_in_CSharp; + +[MemoryDiagnoser(false)] +public class BitArrayBenchmark { - [MemoryDiagnoser(false)] - public class BitArrayBenchmark + [Params(1000, 1000_000, 100_000_000)] + public int Size { get; set; } + + [Benchmark] + public void BitArray() { - [Params(1000, 1000_000, 100_000_000)] - public int Size { get; set; } + BitArray bitArray = new BitArray(Size); + for (int i = 0; i < bitArray.Length; i++) + { + bitArray[i] = true; + } + } - [Benchmark] - public void BitArray() + [Benchmark] + public void BoolArray() + { + bool[] boolArray = new bool[Size]; + for (int i = 0; i < boolArray.Length; i++) { - BitArray bitArray = new BitArray(Size); - for (int i = 0; i < bitArray.Length; i++) - { - bitArray[i] = true; - } + boolArray[i] = true; } + } +} + +[MemoryDiagnoser(false)] +public class BitArrayAndBenchmark +{ + private BitArray _bitsLeft = null!; + private BitArray _bitsRight = null!; + private bool[] _boolsLeft = null!; + private bool[] _boolsRight = null!; + + [Params(1000, 1000_000, 100_000_000)] + public int Size { get; set; } + + [GlobalSetup] + public void Setup() + { + _bitsLeft = new BitArray(Size, true); + _bitsRight = new BitArray(Size, true); + _boolsLeft = new bool[Size]; + _boolsRight = new bool[Size]; + Array.Fill(_boolsLeft, true); + Array.Fill(_boolsRight, true); + } - [Benchmark] - public void BoolArray() + [Benchmark] + public void BitArrayAnd() + { + _bitsLeft.And(_bitsRight); + } + + [Benchmark] + public void BoolArrayAnd() + { + for (int i = 0; i < _boolsLeft.Length; i++) { - bool[] boolArray = new bool[Size]; - for (int i = 0; i < boolArray.Length; i++) - { - boolArray[i] = true; - } + _boolsLeft[i] &= _boolsRight[i]; } } } diff --git a/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArrayExtensionMethods.cs b/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArrayExtensionMethods.cs index 8fa848da74..e16ad4d1ff 100644 --- a/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArrayExtensionMethods.cs +++ b/collections-csharp/BitArray in CSharp/BitArray in CSharp/BitArrayExtensionMethods.cs @@ -1,16 +1,15 @@ using System.Collections; -namespace BitArray_in_CSharp +namespace BitArray_in_CSharp; + +public static class BitArrayExtensionMethods { - public static class BitArrayExtensionMethods + public static void Print(this BitArray bitArray) { - public static void Print(this BitArray bitArray) + foreach (bool bit in bitArray) { - foreach (bool bit in bitArray) - { - Console.Write(bit + " "); - } - Console.WriteLine(); + Console.Write(bit + " "); } + Console.WriteLine(); } } diff --git a/collections-csharp/BitArray in CSharp/BitArray in CSharp/Examples.cs b/collections-csharp/BitArray in CSharp/BitArray in CSharp/Examples.cs new file mode 100644 index 0000000000..7154588b3e --- /dev/null +++ b/collections-csharp/BitArray in CSharp/BitArray in CSharp/Examples.cs @@ -0,0 +1,130 @@ +using System.Collections; +using System.Numerics; + +namespace BitArray_in_CSharp; + +public static class Examples +{ + public static void Run() + { + // Constructors + BitArray bitArray1 = new BitArray(8); + bitArray1.Print(); + + BitArray bitArray2 = new BitArray(8, true); + bitArray2.Print(); + + BitArray bitArray3 = new BitArray(new byte[] { 2 }); + bitArray3.Print(); + + BitArray bitArray4 = new BitArray(new int[] { 4 }); + bitArray4.Print(); + + BitArray bitArray5 = new BitArray(new bool[8]); + bitArray5.Print(); + + BitArray bitArray6 = new BitArray(bitArray1); + bitArray6.Print(); + + Console.WriteLine("--- properties"); + BitArray bitArray = new BitArray(8); + Console.WriteLine(bitArray.Count); + Console.WriteLine(bitArray.IsReadOnly); + Console.WriteLine(bitArray.IsSynchronized); + bitArray[0] = true; + Console.WriteLine(bitArray[0]); + Console.WriteLine(bitArray.Length); + bitArray.Length = 6; + Console.WriteLine(bitArray.Length); + bitArray.Length = 10; + bitArray.Print(); + lock (bitArray.SyncRoot) + { + foreach (var item in bitArray) + { + } + } + + Console.WriteLine("--- methods"); + bitArray.Length = 8; + Console.WriteLine(bitArray.Get(0)); + bitArray.Set(0, true); + bitArray.SetAll(true); + bitArray.Print(); + + Console.WriteLine("--- logical"); + LogicalMethods(); + + Console.WriteLine("--- HasAllSet/HasAnySet"); + BitArray flags = new BitArray(4); + Console.WriteLine(flags.HasAnySet()); + flags[2] = true; + Console.WriteLine(flags.HasAnySet()); + Console.WriteLine(flags.HasAllSet()); + + Console.WriteLine("--- CopyTo"); + CopyToBytes(); + + Console.WriteLine("--- count set bits"); + Console.WriteLine(CountSetBits(new BitArray(new byte[] { 2, 255 }))); + + Console.WriteLine("--- length mismatch"); + try + { + new BitArray(4).And(new BitArray(8)); + } + catch (ArgumentException ex) + { + Console.WriteLine(ex.GetType().Name); + } + + Console.WriteLine("--- index past end"); + try + { + _ = new BitArray(2)[3]; + } + catch (ArgumentException ex) + { + Console.WriteLine(ex.GetType().Name); + } + } + + public static void LogicalMethods() + { + BitArray bitArray1 = new BitArray(new bool[] { false, false, true, true }); + BitArray bitArray2 = new BitArray(new bool[] { false, true, true, false }); + + bitArray1.And(bitArray2).Print(); + bitArray1.Or(bitArray2).Print(); + bitArray1.Xor(bitArray2).Print(); + bitArray2.LeftShift(1).Print(); + bitArray2.RightShift(1).Print(); + bitArray2.Not().Print(); + } + + public static void CopyToBytes() + { + BitArray bits = new BitArray(8); + bits[1] = true; + bits[3] = true; + + byte[] bytes = new byte[1]; + bits.CopyTo(bytes, 0); + + Console.WriteLine(bytes[0]); // 10 + } + + public static int CountSetBits(BitArray bits) + { + int[] words = new int[(bits.Length + 31) / 32]; + bits.CopyTo(words, 0); + + int count = 0; + foreach (int word in words) + { + count += BitOperations.PopCount((uint)word); + } + + return count; + } +} diff --git a/collections-csharp/BitArray in CSharp/BitArray in CSharp/Program.cs b/collections-csharp/BitArray in CSharp/BitArray in CSharp/Program.cs index ed0d7a04fb..d72358dadd 100644 --- a/collections-csharp/BitArray in CSharp/BitArray in CSharp/Program.cs +++ b/collections-csharp/BitArray in CSharp/BitArray in CSharp/Program.cs @@ -1,4 +1,11 @@ using BenchmarkDotNet.Running; using BitArray_in_CSharp; -BenchmarkRunner.Run(); \ No newline at end of file +if (args is ["--bench", .. var benchmarkArgs]) +{ + BenchmarkSwitcher.FromAssembly(typeof(BitArrayBenchmark).Assembly).Run(benchmarkArgs); +} +else +{ + Examples.Run(); +} diff --git a/collections-csharp/BitArray in CSharp/Tests/BitArrayInCSharpUnitTests.cs b/collections-csharp/BitArray in CSharp/Tests/BitArrayInCSharpUnitTests.cs index a841ec6ac3..f0a64aa70c 100644 --- a/collections-csharp/BitArray in CSharp/Tests/BitArrayInCSharpUnitTests.cs +++ b/collections-csharp/BitArray in CSharp/Tests/BitArrayInCSharpUnitTests.cs @@ -159,5 +159,61 @@ public void WhenUsingXorMethod_ThenTheCurrentObjectIsModefied() && bitArray1[2] == false && bitArray1[3] == true); } + + [Fact] + public void WhenUsingConstructorArrayOfBytes_ThenIndexZeroIsTheLeastSignificantBit() + { + var bitArray = new BitArray(new byte[] { 2 }); + + Assert.False(bitArray[0]); + Assert.True(bitArray[1]); + } + + [Fact] + public void WhenUsingLeftShiftMethod_ThenBitsMoveToHigherIndexes() + { + var bitArray = new BitArray(new bool[] { false, true, true, false }); + + bitArray.LeftShift(1); + + Assert.Equal(new[] { false, false, true, true }, bitArray.Cast()); + } + + [Fact] + public void WhenUsingHasAnySetAndHasAllSet_ThenTheyReflectTheBits() + { + var bitArray = new BitArray(4); + bitArray[2] = true; + + Assert.True(bitArray.HasAnySet()); + Assert.False(bitArray.HasAllSet()); + } + + [Fact] + public void WhenUsingCopyToByteArray_ThenBitsPackIntoTheByte() + { + var bitArray = new BitArray(8); + bitArray[1] = true; + bitArray[3] = true; + var bytes = new byte[1]; + + bitArray.CopyTo(bytes, 0); + + Assert.Equal(10, bytes[0]); + } + + [Fact] + public void WhenCountingSetBits_ThenPopCountOfTheWordsIsReturned() + { + var bitArray = new BitArray(new byte[] { 2, 255 }); + + Assert.Equal(9, BitArray_in_CSharp.Examples.CountSetBits(bitArray)); + } + + [Fact] + public void WhenUsingAndWithDifferentLengths_ThenThrowsArgumentException() + { + Assert.Throws(() => new BitArray(4).And(new BitArray(8))); + } } -} \ No newline at end of file +} diff --git a/collections-csharp/BitArray in CSharp/Tests/Tests.csproj b/collections-csharp/BitArray in CSharp/Tests/Tests.csproj index 9928ef50cd..8742ecbc45 100644 --- a/collections-csharp/BitArray in CSharp/Tests/Tests.csproj +++ b/collections-csharp/BitArray in CSharp/Tests/Tests.csproj @@ -1,7 +1,7 @@ - net7.0 + net10.0 enable enable @@ -9,16 +9,20 @@ - - - + + + runtime; build; native; contentfiles; analyzers; buildtransitive all - + runtime; build; native; contentfiles; analyzers; buildtransitive all + + + +