开发者

How does this C# operator work in this code snippet?

开发者 https://www.devze.com 2023-02-05 08:51 出处:网络
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;

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.


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;


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.


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.


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.


& in this case is a bitwise and operator.


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.


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.


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.


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
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号