.NET 相当于 Java 的 Integer.bitCount?

发布于 2024-11-05 14:30:29 字数 1550 浏览 5 评论 0 原文

有没有类似Java的方法 Integer.bitCount(int)Long.bitCount(long) .NET Framework 中的任何位置?

(对于那些不熟悉这些 Java 方法的人)这也称为:

  • 汉明权重
  • 人口计数 (在硬件中实现时通常称为POPCNT。)

虽然那里 很多 实现 在网上找到,我想知道是否有标准库实现。

我知道这不在 BitArrayUInt32BitConverter 中,但也许有一个版本隐藏在某个地方,例如在加密函数中。

Is there a method similar to Java's Integer.bitCount(int) or Long.bitCount(long) anywhere in the .NET Framework?

(For those unfamiliar with these Java methods) this is also known as:

  • Hamming Weight
  • Population Count (often called POPCNT when implemented in hardware.)

Although there are plenty of implementations to be found on the web, I was wondering if there was a standard library implementation.

I know this is not in BitArray, UInt32 or BitConverter, but maybe there's a version hidden somewhere, e.g. in a crypto function.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

我恋#小黄人 2024-11-12 14:30:30

此功能不在 .NET Framework 或 .NET Standard 中,但在 .NET Core 3.0 及更高版本中,因此包括 .NET 5.0 及更高版本,位于 System.Numerics.BitOperations 静态类,特别是方法

两者都返回 System.Int32,即 C# 中的 int

还有其他有用的操作:计算前导零或尾随零、计算整数以 2 为底的对数以及执行位旋转(也称为循环移位)。

在核心库中这样做的最大好处/原因可能是您可以获得硬件加速而无需链接到非托管代码,并且类文档证实了这一点:

为内在的位旋转操作提供实用方法。这些方法使用底层平台上可用的硬件内在函数;否则,他们会使用优化的软件后备。

This functionality is not in .NET Framework nor .NET Standard but it is in .NET Core 3.0 and newer, thus including .NET 5.0 and newer, under the System.Numerics.BitOperations static class, in particular the methods

both of which return System.Int32 aka int in C#.

There are also other useful operations: count leading or trailing zeros, compute integer base-2 logarithm, and perform bit rotation aka circular shift.

Possibly the biggest benefit of/reason for doing this in the core libraries is that you can get hardware acceleration without linking to unmanaged code, and the class docs confirm this:

Provides utility methods for intrinsic bit-twiddling operations. The methods use hardware intrinsics when available on the underlying platform; otherwise, they use optimized software fallbacks.

习ぎ惯性依靠 2024-11-12 14:30:30

我知道这是一个非常古老的问题,但对于像我这样的人来说,至少有一个解决方法可能会有所帮助:

public static int BitCount(int n)
{
    var count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1); //walking through all the bits which are set to one
    }

    return count;
}

I know it's a very old question but it might be helpful for someone like me to have at least a workaround for this:

public static int BitCount(int n)
{
    var count = 0;
    while (n != 0)
    {
        count++;
        n &= (n - 1); //walking through all the bits which are set to one
    }

    return count;
}
享受孤独 2024-11-12 14:30:30

BitVector32 也不是 BitArray类也有这样的方法,所以我相信框架中确实缺少这个方法。

就我个人而言,我认为这些类并不是真正有用,因为它们错过了许多自然的位操作。我不确定它们的真正用途是什么。事实上,它们的用处非常有限。

Neither the BitVector32 nor the BitArray classes have such a method either so I believe that this method is indeed missing from the framework.

Personally, I think these classes aren’t really useful anyway since they miss many natural bit operations. I’m not sure what they are really intended for. As it is, their usefulness very limited.

零崎曲识 2024-11-12 14:30:30

这些方法基于 Hacker's Delight 的算法。您可以在此处下载它们的 C 代码。

These methods are based on algorithms from Hacker's Delight. You can download C code for them here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文