首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法解释此BitVector32的状态

无法解释此BitVector32的状态
EN

Stack Overflow用户
提问于 2020-09-26 00:36:41
回答 1查看 69关注 0票数 0

我现在正在调试一些代码(VS 2019,.NET Framework4.7.2),在断点停止,使用立即窗口计算变量。我有一个BitVector32,我不了解它的状态。以下是IW的内容

代码语言:javascript
复制
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

没有调用任何Create*方法,stillInHand是用BitVector32(Int32) ctor创建的。索引2和3不应该是true,其余的都应该是false

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-28 09:54:03

实际上是,这个问题关系到对BitVector32[ ]索引的理解。

首先stillInHand[1]并不意味着获得第二位stillInHand(BitVector32)。它表示此操作:使用00 00 … 00 01使用stillInHand(BitVector32)执行&(和)操作。

的例子是stillInHand(BitVector32)00 00 00 00 00 … 00 00 00 11 00100 00 00 00 00 … 00 00 00 00 01。然后执行&(和)操作。

代码语言:javascript
复制
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,因此如果输出或看到stillInHand[1]的结果,结果将是false

So,对于stillInHand[2],您可以看到

代码语言:javascript
复制
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],您可以看到

代码语言:javascript
复制
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

实际上是,如果您从这里分析源代码:参考源,您可以看到以下代码:

代码语言:javascript
复制
/// <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,这将很容易理解。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64072807

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档