Skip to content
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<RootNamespace>BitArray_in_CSharp</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.13.2" />
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -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];
}
}
}
Original file line number Diff line number Diff line change
@@ -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();
}
}
130 changes: 130 additions & 0 deletions collections-csharp/BitArray in CSharp/BitArray in CSharp/Examples.cs
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
using BenchmarkDotNet.Running;
using BitArray_in_CSharp;

BenchmarkRunner.Run<BitArrayBenchmark>();
if (args is ["--bench", .. var benchmarkArgs])
{
BenchmarkSwitcher.FromAssembly(typeof(BitArrayBenchmark).Assembly).Run(benchmarkArgs);
}
else
{
Examples.Run();
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<bool>());
}

[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<ArgumentException>(() => new BitArray(4).And(new BitArray(8)));
}
}
}
}
14 changes: 9 additions & 5 deletions collections-csharp/BitArray in CSharp/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net7.0</TargetFramework>
<TargetFramework>net10.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="18.11.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="4.0.1">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
<PackageReference Include="coverlet.collector" Version="3.1.2">
<PackageReference Include="coverlet.collector" Version="10.1.0">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\BitArray in CSharp\BitArray in CSharp.csproj" />
</ItemGroup>

</Project>
Loading