|=(单管道相等)和 &=(单 & 符号相等)是什么意思
在下面几行中:
//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# 中意味着什么?
我想删除系统属性并保留其他属性......
In below lines:
//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;
What does |=
(single pipe equal) and &=
(single ampersand equal) mean in C#?
I want to remove system attribute with keeping the others...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
它们是 复合赋值运算符,翻译(非常松散)
为
和 与
&
相同。在一些关于隐式转换的情况下有更多细节,并且目标变量仅被评估一次,但这基本上就是它的要点。就非复合运算符而言,
&
是按位“AND” 和|
是按位“OR”。编辑:在这种情况下,您需要
Folder.Attributes &= ~FileAttributes.System
。要理解原因:~FileAttributes.System
表示“除System
之外的所有属性”(~
是按位非)&
表示“结果是操作数两侧出现的所有属性”,因此它基本上充当掩码 - 仅保留出现在 ( “除了系统之外的一切”)。一般来说:
|=
只会向目标添加位&=
只会删除位目标They're compound assignment operators, translating (very loosely)
into
and the same for
&
. There's a bit more detail in a few cases regarding an implicit cast, and the target variable is only evaluated once, but that's basically the gist of it.In terms of the non-compound operators,
&
is a bitwise "AND" and|
is a bitwise "OR".EDIT: In this case you want
Folder.Attributes &= ~FileAttributes.System
. To understand why:~FileAttributes.System
means "all attributes exceptSystem
" (~
is a bitwise-NOT)&
means "the result is all the attributes which occur on both sides of the operand"So it's basically acting as a mask - only retain those attributes which appear in ("everything except System"). In general:
|=
will only ever add bits to the target&=
will only ever remove bits from the target|
是 按位或&
是 按位与a |= b
相当于a = a | b
除了a
仅计算一次a &= b
相当于a = a & b
除外,a
仅计算一次。为了删除系统位而不更改其他位,请使用
~
按位求反。因此,您可以将除系统位之外的所有位设置为 1。and
-使用掩码将系统设置为 0 并保持所有其他位不变,因为0 & x = 0 和 1 & x = x
对于任何x
|
is bitwise or&
is bitwise anda |= b
is equivalent toa = a | b
except thata
is evaluated only oncea &= b
is equivalent toa = a & b
except thata
is evaluated only onceIn order to remove the System bit without changing other bits, use
~
is bitwise negation. You will thus set all bits to 1 except the System bit.and
-ing it with the mask will set System to 0 and leave all other bits intact because0 & x = 0
and1 & x = x
for anyx
您可以这样做:
You can do this like so: