0

Can you recommend a quick way to generate an Hashcode for a BitArray of size N > 1k, in C# .Net Core 3.1?

I've looked around and found a few answers but were specific to scenario under 64 bits and about a decade old. The few candidates that I have so far are:

  1. Convert to string and use GetHashCode(). I'm a bit worried about the memory and speed here

  2. Split my BitArray by chunk of 64 bits, convert them to long, call GetHasCode() then combine their hash with

unchecked
{
    int hash = 17;
    hash = hash * 31 + firstLong.GetHashCode();
    hash = hash * 31 + secondLong.GetHashCode();
    // etc
    return hash;
}

I'm not really sure how to split effectively my BitArray into long and it's allocating many object here too

  1. use Value tuple feature since C# 7: (myBitArray).GetHashCode(); I'm not sure how it will work to be honest, but it seems to work on the Stack only
  • What's the purpose? Would any of the fast hashing algorithms work for you - there's good analysis here: https://aras-p.info/blog/2016/08/02/Hash-Functions-all-the-way-down/ - some of the popular algorithms mentioned there have c# implementations, at least I looked at mumur implementation: https://github.com/darrenkopp/murmurhash-net – Giedrius Apr 23 '20 at 13:39
  • Provide a Stream adapter on top of your bit vector, and use out of the box classes such as md5 etc – Roman Polunin Apr 23 '20 at 13:56
  • BitArray is a .NET 1.1 collection, and as such has no generics. I'm guessing it's gonna be prone to boxing, so performance is always going to be an issue with it. Your best bet is going to be copying it to a byte array and then computing the hash code of that array incrementally. – Etienne de Martel Apr 23 '20 at 14:23

0 Answers0