Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# |=(单管等于)和&=(单安培)平均值_C#_Operators_Bitwise Operators - Fatal编程技术网

C# |=(单管等于)和&=(单安培)平均值

C# |=(单管等于)和&=(单安培)平均值,c#,operators,bitwise-operators,C#,Operators,Bitwise Operators,在下面几行: //Folder.Attributes = FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly; Folder.Attributes |= FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly;

在下面几行:

//Folder.Attributes = FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly;
Folder.Attributes |= FileAttributes.Directory | FileAttributes.Hidden | FileAttributes.System | FileAttributes.ReadOnly;


Folder.Attributes |= ~FileAttributes.System;
Folder.Attributes &= ~FileAttributes.System;
|=
(单管等效)和
&=
(单管等效)在C#中是什么意思

我想删除系统属性,保留其他属性…

它们是运算符,正在转换(非常松散)

进入

对于
也一样。在一些情况下,关于隐式强制转换有更多的细节,目标变量只计算一次,但这基本上是它的要点

在非复合运算符方面,以及

编辑:在这种情况下,您需要
Folder.Attributes&=~FileAttributes.System
。要了解原因:

  • ~FileAttributes.System
    表示“除
    System
    之外的所有属性”(
    ~
    是按位NOT)
  • &
    表示“结果是操作数两侧出现的所有属性”
所以它基本上就像一个面具——只保留那些出现在(“除系统之外的一切”)中的属性。一般而言:

  • |=
    将只向目标添加位
  • &=
    将只从目标中删除位
      • |
      • &
      a |=b
      等同于
      a=a | b
      ,除了
      a
      只计算一次
      a&=b
      等同于
      a=a&b
      ,只是
      a
      只计算一次

      要在不更改其他位的情况下删除系统位,请使用

      Folder.Attributes &= ~FileAttributes.System;
      
      ~
      是按位求反。因此,除系统位外,将所有位设置为1
      -使用掩码将其设置为0,并保留所有其他位不变,因为对于任何
      x

      我想删除系统属性并保留其他属性

      您可以这样做:

      Folder.Attributes ^= FileAttributes.System;
      

      x=x |(y)
      是一种更好的描述方式,因为
      x |=y+z
      x=x | y+z不同感谢您的回答/但出于我的目的(删除系统属性),我应该使用哪一个(|=或&=)?@LostLord:
      Folder.Attributes&=~FileAttributes.system我认为您应该使用XOR而不是AND。有点困惑/~是必要的,或者not@LostLord据我所知,这两种方法是相似的aware@ChrisS
      ^=bit
      将设置尚未设置的位,
      &=~bit
      未设置。您肯定不想使用异或。如果它不见了,那会把它放回去。
      a
      只计算一次是什么意思?为什么评估次数会比这多?@silkfire这叫做短路评估,请看@Polluks,所以基本上
      a |=b
      实际上意味着
      a=a | | b
      ?@silkfire是的,但不要互换一根管道和两根管道。@Polluks:我不理解你关于一根管道和两根管道的评论,我认为使用两根管道而不是一根管道是silkfire的全部观点。此外,我不相信“除了
      a
      只计算一次”这句话确实指的是短路计算,因为短路计算不会改变第一个操作数(
      a
      )的计算,但可能会跳过第二个操作数(
      b
      )的计算。
      Folder.Attributes &= ~FileAttributes.System;
      
      Folder.Attributes ^= FileAttributes.System;