这个 C# 运算符在这段代码片段中是如何工作的?
我在 SO 上找到了这个代码片段(抱歉,我没有问题/答案组合的链接)
bool isDir = (File.GetAttributes(source) & FileAttributes.Directory) == FileAttributes.Directory;
这让我很困惑,因为 FileAttributes.Directory
位于 ==.
在这种情况下 &
有何作用?我不知道如何阅读这行代码。我正在尝试评估路径字符串是文件还是目录。
I found this code snippet on SO (sorry I don't have the link to the question/answer combo)
bool isDir = (File.GetAttributes(source) & FileAttributes.Directory) == FileAttributes.Directory;
This confuses me because FileAttributes.Directory
is on both sides of the ==
.
What does the &
do in this case? I'm not sure how to read this line of code. I'm trying to evaluate whether a path string is a file or a directory.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
它正在测试
File.GetAttributes
返回的枚举中是否设置了FileAttributes.Directory
标志。您可以在 MSDN 上的此条目。我宁愿使用 System.IO 中的方法之一,例如
目录.Exists
:It's testing whether the flag
FileAttributes.Directory
is set in the enum returned byFile.GetAttributes
. You can read more about how to use flag enums in this entry on MSDN.I'd rather use one of the methods in System.IO, like
Directory.Exists
:GetAttributes 返回一个标志值,其中每个位代表不同的布尔状态。此代码使用按位 &运算符,除了 GetAttributes 返回的任何其他标志之外,还可以打开目录标志。
这似乎过于复杂了。这相当于编写:
或者,专门测试 Directory 属性:
HasFlag() 方法当前有点慢,因此按位替代方法更快并且使用更少的资源。 HasFlag 非常适合快速、轻松地响应标志中所需的位是打开还是关闭,而无需了解位值或二进制。
GetAttributes returns a flag value, where each bit represents a different Boolean state. This code uses the bitwise & operator, to turn on the Directory flag, in addition to any other flags returned by GetAttributes.
This appears to have been over-complicated. This is equivalent to writing:
Or, to test exclusively for the Directory attribute:
The HasFlag() method is currently a little slow, so a bitwise alternative is faster and uses less resources. HasFlag is nice for getting a quick and easy response to whether the desired bit in the flag is on or off, without any knowledge of the bit values or binary in general.
它使用位掩码来测试是否设置了单个位(FileAttributes.Directory)。
枚举的值是 2 的幂,对应于各个位。
如果设置了 ReadOnly 和 Directory,则 FileAttributes 等于 17。计算结果以二进制形式表示如下:
如果未设置 Directory 位,您将得到零:
一种稍微更简洁的编写方法给出相同效果的表达式是针对零进行测试:
It is using a bit mask to test if a single bit (FileAttributes.Directory) is set.
The values of the enum are powers of two, corresponding to individual bits.
If ReadOnly and Directory are set then FileAttributes is equal to 17. The calculation looks like this in binary:
If the Directory bit was not set you'd get zero instead:
A slightly more concise way to write the expression that gives the same effect is to test against zero:
它执行按位与运算。属性存储为位标志,因此将这些标志与 AttributeFlags.Directory 一起使用,以查看其中一个属性是否为 .Directory。
位标志的好例子在这里:
http://weblogs.asp.net/wim/archive/2004 /04/07/109095.aspx
那么:
0100 与目录标志相同,所以我们现在知道该标志位于枚举的选定标志中。
its doing a Bitwise AND operation. Attributes are stored as bit flags, so it is and'ing those flags together with AttributeFlags.Directory to see if one of the attributes is .Directory.
Good example of Bit Flags here:
http://weblogs.asp.net/wim/archive/2004/04/07/109095.aspx
Then:
0100 is the same as the directory flag, so we now know that that flag is in the chosen flags of the enum.
这是逻辑&运算符。在此特定示例中,它检查 FileAttributes 枚举是否具有Directory 值,验证
source
变量指向的字符串是否是目录。It is the logical & operator. In this particular example it checks if the FileAttributes enumeration has the Directory value, verifying if the string pointed by the
source
variable is a directory.单曲&是位运算符。
http://msdn.microsoft.com/en- us/library/sbf85k1c(v=VS.100).aspx
它对两个值的各个位执行按位与。它在位掩码中被大量使用。
The single & is a bitwise operator.
http://msdn.microsoft.com/en-us/library/sbf85k1c(v=VS.100).aspx
It performs a bitwise AND on the individual bits for the two values. It is used a lot in bit masks.
在本例中,
&
是按位and
运算符。&
in this case is a bitwiseand
operator.它正在执行按位标志测试 -
File.GetAttributes(source)
可以返回数量 的标志(以不同的位表示),指示不同的属性。&
将1
限制为 仅FileAttributes.Directory
中存在的内容(我希望这个成为一个位)。碰巧,这是16
,即(二进制)..0001000
,如果
源
具有ReadOnly
(= 1)、隐藏
(=2) 和目录
(=16) 它将是:我们 & 16
离开,
因此目录测试通过。
相反,如果源具有
System
(=4) 和ReadOnly
(=1)(而不是目录),则它将是:we & 16
离开,
因此目录测试失败。
作为旁注;此类测试中的
==
验证是否设置了所有所需标志(如果第二个操作数中有多个位)。另一个常见的测试是!= 0
,它测试是否存在任何位。It is performing a bitwise flag test -
File.GetAttributes(source)
could return a number of flags (in different bits) indicating different properties. The&
restricts the1
s to just those that are present inFileAttributes.Directory
(I would expect this to be a single bit). As it happens, this is16
, i.e. (binary)..0001000
if the
source
hasReadOnly
(=1),Hidden
(=2) andDirectory
(=16) it would be:we & with 16
leaving
hence the directory test passes.
If instead the source has
System
(=4) andReadOnly
(=1) (and not directory) it will be:we & with 16
leaving
hence the directory test fails.
As a side note; an
==
in such a test verifies that all the required flags were set (if multiple bits were in the second operand). Another common test is!= 0
, which tests whether any bits were present.它是按位运算符 AND。
http://en.wikipedia.org/wiki/Bitwise_operation
codesnippet 在两个之间执行按位与变量,然后将值与另一个变量进行比较,将结果放入布尔值中。
It is the bitwise operator AND.
http://en.wikipedia.org/wiki/Bitwise_operation
The codesnippet performs a bitwise AND between two variables and then compares the value to another variable, placing the result in a bool.
它是按位
AND
运算。FileAttributes
是 Flags 枚举。这意味着该枚举的数值中的每一位都描述了该文件的一些布尔属性,并且它们可以组合起来。It is bitwise
AND
operation.FileAttributes
is Flags enum. That means that each bit in numeric value of this enum describes some boolean property of this file and them could be combined.