.NET equivalent of Java's Integer.bitCount?
Asked Answered
C

4

16

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.

Canticle answered 6/5, 2011 at 10:37 Comment(0)
F
4

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.

Faus answered 6/5, 2011 at 10:42 Comment(4)
Useful for efficient implementation of Hash array mapped trie: en.wikipedia.org/wiki/Hash_array_mapped_trieKellikellia
@David My gripe was that the two classes have poorly designed interfaces. Of course you can use them to develop certain things but you can also use a plain bit vector (int) almost as well. A proper interface would make the types way more useful.Faus
@gjsduarte you might change your mind if you meet a problem that can be easily solved with bitCount() in a job interview....(Apparently the interview himself thought this method exist in .NET)Chainman
@Chainman I think the agreement was over the fact that the .NET classes are useless, not that a bit count method would be.Faus
S
6

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.

Spree answered 24/5, 2020 at 17:23 Comment(0)
F
5

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;
}
Fantastic answered 8/6, 2020 at 14:49 Comment(0)
F
4

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.

Faus answered 6/5, 2011 at 10:42 Comment(4)
Useful for efficient implementation of Hash array mapped trie: en.wikipedia.org/wiki/Hash_array_mapped_trieKellikellia
@David My gripe was that the two classes have poorly designed interfaces. Of course you can use them to develop certain things but you can also use a plain bit vector (int) almost as well. A proper interface would make the types way more useful.Faus
@gjsduarte you might change your mind if you meet a problem that can be easily solved with bitCount() in a job interview....(Apparently the interview himself thought this method exist in .NET)Chainman
@Chainman I think the agreement was over the fact that the .NET classes are useless, not that a bit count method would be.Faus
M
0

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

Material answered 6/5, 2011 at 10:47 Comment(3)
As mentioned in the question, there is no shortage of methods I could download to do the job. The question is specifically about standard library methods. Java has them, I don't think .NET does (but I hope to be proved wrong.)Canticle
Yeah, I know there are tons of implementations. This is as close to a library that I know of, though, given the source.Material
Here's a link to the code in the wayback machine since this one is expired: web.archive.org/web/20190108163330/http://…Inulin

© 2022 - 2024 — McMap. All rights reserved.