开发者

A problem with using System.Collections.Specialized.BitVector32: a bug?

开发者 https://www.devze.com 2023-01-06 09:06 出处:网络
I\'m trying to do a short simulation where i needed a small bit array, and I chose System.Collections.Specialized.BitVector32.

I'm trying to do a short simulation where i needed a small bit array, and I chose System.Collections.Specialized.BitVector32.

I'm running it inside a single-threaded object, in a single-threaded loop about 1,000,000 times, each time for indexes {0,1,2}.

Here is the code:

private System.Collections.Specialized.BitVector32 currentCalc 
    = new System.Collections.Specialized.BitVector32();

private void storeInCurrent(int idx, bool val)
{
    currentCalc[idx] = val;
    if (currentCalc[idx] != val)
    {
        throw new Exception("Inconceivable!");
    }
}

To my understanding, the exception should not be thrown, but sometimes it does! An exception is not thrown every time, but in a fair percent - a CONSTANT 1/6 of the time! (which is even stranger)

What am I d开发者_运维技巧oing wrong?


Look at MSDN; the indexer takes the mask, not the index. So that is:

int mask = 1 << idx;

then use currentCalc[mask]

This is odd though; if you are happy enough to use masks - why would one be using BitVector32, rather than just an int. I also assumed the indexer would take the index. VERY odd design decision.

0

精彩评论

暂无评论...
验证码 换一张
取 消