dotnet / runtime

.NET is a cross-platform runtime for cloud, mobile, desktop, and IoT apps.
https://docs.microsoft.com/dotnet/core/
MIT License
14.95k stars 4.65k forks source link

[API Proposal]: Adding a Cardinality/PopCount property to BitArray #104299

Open polijp opened 2 months ago

polijp commented 2 months ago

Background and motivation

You often need to know how many bits are set to 1 in a BitArray. Presently, there are two ways to achieve that:

Adding a Cardinality property takes the advantage of the underlying int array. It could use the instrinsic BitOperations.PopCount() method.

API Proposal

namespace System.Collections;

public class BitArray ...
{
    public int PopCount => ...
}

API Usage


// Fancy the value
var c = new BitArray(10);
if (c.PopCount %2 ==0) do something;

### Alternative Designs

_No response_

### Risks

_No response_
dotnet-policy-service[bot] commented 2 months ago

Tagging subscribers to this area: @dotnet/area-system-collections See info in area-owners.md if you want to be subscribed.