这个 C# 运算符在这段代码片段中是如何工作的?

发布于 2024-10-12 20:59:04 字数 332 浏览 10 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

逆光飞翔i 2024-10-19 20:59:05

它正在测试 File.GetAttributes 返回的枚举中是否设置了 FileAttributes.Directory 标志。您可以在 MSDN 上的此条目。

我正在尝试评估路径字符串是文件还是目录。

我宁愿使用 System.IO 中的方法之一,例如 目录.Exists

if (Directory.Exists(path))
{
    // it's a directory
} 
else if (File.Exists(path))
{
    // it's a file
}
else
{
   // doesn't exist
}

It's testing whether the flag FileAttributes.Directory is set in the enum returned by File.GetAttributes. You can read more about how to use flag enums in this entry on MSDN.

I'm trying to evaluate whether a path string is a file or a directory.

I'd rather use one of the methods in System.IO, like Directory.Exists:

if (Directory.Exists(path))
{
    // it's a directory
} 
else if (File.Exists(path))
{
    // it's a file
}
else
{
   // doesn't exist
}
如歌彻婉言 2024-10-19 20:59:05

GetAttributes 返回一个标志值,其中每个位代表不同的布尔状态。此代码使用按位 &运算符,除了 GetAttributes 返回的任何其他标志之外,还可以打开目录标志。

这似乎过于复杂了。这相当于编写:

bool isDir = File.GetAttributes(source).HasFlag(FileAttributes.Directory);

或者,专门测试 Directory 属性:

bool isDir = File.GetAttributes(source) == FileAttributes.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:

bool isDir = File.GetAttributes(source).HasFlag(FileAttributes.Directory);

Or, to test exclusively for the Directory attribute:

bool isDir = File.GetAttributes(source) == FileAttributes.Directory;

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.

万水千山粽是情ミ 2024-10-19 20:59:04

它使用位掩码来测试是否设置了单个位(FileAttributes.Directory)。

枚举的值是 2 的幂,对应于各个位。

    ReadOnly = 1,
    Hidden = 2,
    System = 4,
    Directory = 16,
    Archive = 32,
    Device = 64,

如果设置了 ReadOnly 和 Directory,则 FileAttributes 等于 17。计算结果以二进制形式表示如下:

File.GetAttributes(source) = 00001001   
FileAttributes.Directory   = 00001000 &
-------------------------------------
                             00001000

如果未设置 Directory 位,您将得到零:

File.GetAttributes(source) = 00000001   
FileAttributes.Directory   = 00001000 &
-------------------------------------
                             00000000

一种稍微更简洁的编写方法给出相同效果的表达式是针对零进行测试:

bool isDir = (File.GetAttributes(source) & FileAttributes.Directory) != 0;

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.

    ReadOnly = 1,
    Hidden = 2,
    System = 4,
    Directory = 16,
    Archive = 32,
    Device = 64,

If ReadOnly and Directory are set then FileAttributes is equal to 17. The calculation looks like this in binary:

File.GetAttributes(source) = 00001001   
FileAttributes.Directory   = 00001000 &
-------------------------------------
                             00001000

If the Directory bit was not set you'd get zero instead:

File.GetAttributes(source) = 00000001   
FileAttributes.Directory   = 00001000 &
-------------------------------------
                             00000000

A slightly more concise way to write the expression that gives the same effect is to test against zero:

bool isDir = (File.GetAttributes(source) & FileAttributes.Directory) != 0;
对岸观火 2024-10-19 20:59:04

它执行按位与运算。属性存储为位标志,因此将这些标志与 AttributeFlags.Directory 一起使用,以查看其中一个属性是否为 .Directory。

位标志的好例子在这里:
http://weblogs.asp.net/wim/archive/2004 /04/07/109095.aspx

[Flags]
public enum FileAttributes
{
  Archive,        // 0000
  Compressed,     // 0001
  Device,         // 0010
  Directory,      // 0100
  Encrypted,       // 1000
  ...
}

那么:

 File.GetAttributes(source):  1101
 FileAttributes.Directory:    0100
 (Logical AND):               0100

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

[Flags]
public enum FileAttributes
{
  Archive,        // 0000
  Compressed,     // 0001
  Device,         // 0010
  Directory,      // 0100
  Encrypted,       // 1000
  ...
}

Then:

 File.GetAttributes(source):  1101
 FileAttributes.Directory:    0100
 (Logical AND):               0100

0100 is the same as the directory flag, so we now know that that flag is in the chosen flags of the enum.

家住魔仙堡 2024-10-19 20:59:04

这是逻辑&运算符。在此特定示例中,它检查 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.

雪落纷纷 2024-10-19 20:59:04

单曲&是位运算符。
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.

鹿! 2024-10-19 20:59:04

在本例中,& 是按位and 运算符。

& in this case is a bitwise and operator.

谁与争疯 2024-10-19 20:59:04

它正在执行按位标志测试 - File.GetAttributes(source) 可以返回数量 的标志(以不同的位表示),指示不同的属性。 &1 限制为 FileAttributes.Directory 中存在的内容(我希望这个成为一个位)。碰巧,这是 16,即(二进制)..0001000

如果 具有 ReadOnly (= 1)、隐藏 (=2) 和目录 (=16) 它将是:

...0001011

我们 & 16

...0001000

离开,

...0001000

因此目录测试通过

相反,如果源具有 System (=4) 和 ReadOnly (=1)(而不是目录),则它将是:

...0000101

we & 16

...0001000

离开,

...0000000

因此目录测试失败

作为旁注;此类测试中的 == 验证是否设置了所有所需标志(如果第二个操作数中有多个位)。另一个常见的测试是!= 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 the 1s to just those that are present in FileAttributes.Directory (I would expect this to be a single bit). As it happens, this is 16, i.e. (binary) ..0001000

if the source has ReadOnly (=1), Hidden (=2) and Directory (=16) it would be:

...0001011

we & with 16

...0001000

leaving

...0001000

hence the directory test passes.

If instead the source has System (=4) and ReadOnly (=1) (and not directory) it will be:

...0000101

we & with 16

...0001000

leaving

...0000000

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.

这样的小城市 2024-10-19 20:59:04

它是按位运算符 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.

醉殇 2024-10-19 20:59:04

它是按位AND运算。 FileAttributes 是 Fl​​ags 枚举。这意味着该枚举的数值中的每一位都描述了该文件的一些布尔属性,并且它们可以组合起来。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文