其实,这个问题与对BitVector32[ ]的索引的理解有关。
首先,stillInHand[1] 并不意味着获得 stillInHand(BitVector32) 的第二位。它代表这个动作:使用00 00 … 00 01 与 StillInHand(BitVector32) 执行&(AND) 操作。
举个例子:stillInHand(BitVector32) 是 00 00 00 00 00 … 00 00 00 11 00,1 是 00 00 00 00 00 … 00 00 00 00 01。然后执行&(AND) 操作。
00 00 00 00 00 … 00 00 00 11 00 12 &(AND)
00 00 00 00 00 … 00 00 00 00 01 `1`
--------------------------------------------
00 00 00 00 00 … 00 00 00 00 00
并且可以看到最后一位(关注索引值1)从1变为0,所以结果会是false,如果你输出或者看到@987654336的结果@。
所以,对于stillInHand[2],你可以看到
00 00 00 00 00 … 00 00 00 11 00 12 &(AND)
00 00 00 00 00 … 00 00 00 00 10 2
--------------------------------------------
00 00 00 00 00 … 00 00 00 00 00
倒数第二位(关注索引值2)从1变为0,所以结果也是false。
对于stillInHand[8],你可以看到
00 00 00 00 00 … 00 00 00 11 00 12 &(AND)
00 00 00 00 00 … 00 00 00 10 00 8
--------------------------------------------
00 00 00 00 00 … 00 00 00 10 00
倒数第四位(关注索引值8)不变,保持为1,所以结果为true。
其实,如果你从这里分析源代码:Reference Source,你可以看到这些代码:
/// <devdoc>
/// <para>Gets or sets a value indicating whether all the specified bits are set.</para>
/// </devdoc>
public bool this[int bit] {
get {
return (data & bit) == (uint)bit;
//clear other bits (to 0) through the AND operation of the data and mask.
//If the result of the operation is equal to the mask, return true.
//Otherwisse, return false.
}
set {
if (value) {
data |= (uint)bit;
//data = data OR mask, set the specified bit to “1”
}
else {
data &= ~(uint)bit;
//data = data AND ~mask, set the specified bit to “0”
}
}
}
当然,你可以把it当成mask,这样就很容易理解了。