I am debugging some code right now (VS 2019, .NET Framework 4.7.2), stopped at a breakpoint, using the Immediate Window to evaluate variables. I have a BitVector32 which I am not understanding its state. Here is the content of the IW:
stillInHand.ToString()
"BitVector32{00000000000000000000000000001100}"
stillInHand
{BitVector32{00000000000000000000000000001100}}
Data: 12
stillInHand[0]
true
stillInHand[1]
false
stillInHand[2]
false
stillInHand[3]
false
stillInHand[4]
true
stillInHand[5]
false
stillInHand[6]
false
stillInHand[7]
false
There have been no calls to any of the Create* methods, and stillInHand was created with the BitVector32(Int32) ctor. Shouldn't indices 2 & 3 be true and all the rest be false?
Actually, this issue is related to the understanding of the index of
BitVector32[ ].First of all,
stillInHand[1]doesn’t mean to get the second bit of stillInHand(BitVector32). It represents this action: use00 00 … 00 01to perform&(AND) operation with stillInHand(BitVector32).For an example:
stillInHand(BitVector32)is00 00 00 00 00 … 00 00 00 11 00, and1is00 00 00 00 00 … 00 00 00 00 01. Then perform&(AND) operation.And you can see that the last bit(focus on the index value
1) changes from1to0, so the result will befalse, if you output or see the result ofstillInHand[1].So, for
stillInHand[2], you can seeThe second to last bit(focus on the index value
2) changes from1to0, so the result will befalsetoo.And for
stillInHand[8], you can seeThe fourth to last bit(focus on the index value
8) doesn’t change and it remains as1, so the result will betrue.Actually, if you analyze the source code from here: Reference Source, you can see these codes:
Of course, you can consider
itasmask, and this will be easy to understand.