Skip to content

Add BitOperations.IsPow2 polyfills #575

Description

@thomhurst

Is the feature request related to a problem

Code targeting older frameworks such as netstandard2.0 cannot use System.Numerics.BitOperations.IsPow2. For example:

var indexMask = BitOperations.IsPow2((uint)capacity)
    ? capacity - 1
    : -1;

The current Polyfill API list does not include BitOperations or IsPow2.

Microsoft documents these overloads:

  • bool IsPow2(int value)
  • bool IsPow2(uint value)
  • bool IsPow2(long value)
  • bool IsPow2(ulong value)
  • bool IsPow2(nint value)
  • bool IsPow2(nuint value)

https://learn.microsoft.com/dotnet/api/system.numerics.bitoperations.ispow2

Describe the solution

Add a conditionally compiled System.Numerics.BitOperations polyfill on targets where the type/API is absent, with matching overloads.

The implementation is self-contained bit arithmetic. Zero and negative signed values return false; positive values use the standard (value & (value - 1)) == 0 test.

Describe alternatives considered

Consumers can add a private helper, but then cross-target source cannot retain the BCL call shape.

A static extension member could preserve the call shape where BitOperations exists but lacks an overload. On targets such as netstandard2.0, the type itself appears absent, so defining the conditional static class may be necessary.

Additional context

This surfaced while evaluating a source-only library for netstandard2.0. The API has no runtime-state dependency and should be fully polyfillable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions