How to do this ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) {} work?

On the this page, I see the following code:

if ((attributes & FileAttributes.Hidden) == FileAttributes.Hidden) 

But I do not understand why it is so.

Why attributes & FileAttributes.Hidden) ? What does a singular attributes check actually do? Does it verify that it is not null? I have a feeling that I know, but it seems strange. Random and weird.

+5
source share
3 answers

Simple logical logic. The == operator only returns true if both sides have the same value. Therefore, you need to mask the value of attributes with the flag with which you want to compare. Consider these two examples (values ​​compiled):

right:

  0010001 // attributes & 0000001 // FileAttributes.Hidden ------- = 0000001 // FileAttributes.Hidden 

false:

  0011000 // attributes & 0000001 // FileAttributes.Hidden ------- = 0000000 // FileAttributes.None 
+15
source

attributes is of type FileAttributes . This type is enum , the type of value. Is this not FileAttributes? ( Nullable<FileAttributes> ), so it just cannot be null .

Each enum consists of a list of named values, but each of them is converted / converted to some integer (int) values. In many places, C # allows you to “use” this conversion and (somewhat) process enum values ​​as if they were ints, but still they are not.

For example, the operator and does what you expect - it executes binary I. Thus, if the enum value converted to integer has the corresponding bit (s), you will get some non-zero result.

In the "good old way" check for the presence of some flags / bits in any status. You can often see an expression like foo & FLAG != 0 , which checks whether the flag is set, or foo & (FLAG|BLAG) != 0 , which checks whether either of these two is set. This is a bit erroneous / dangerous, because if someone changes the FLAG by a few bits, then such expressions will check if the ANY bit is set, and not if such a "multi-bit flag" is set. This is why you can also often see foo & FLAG == FLAG , which applies a bitmask and checks if the IS result is a bitmask, so it checks to see if all bits of the mask are set.

Here, in your case, this is exactly so. Since in this expression you are ANDing and COMPARING with the same mask, you effectively check if ALL mask bits are set. But superfluous, since FileAttributes is clearly indicated as [Flags], the Hidden value has only one bit, so! = 0 will be enough.

However, in such cases (checking flags, not fancy bitmaps) you can try to use the Enum.HasFlag method, most people will advise you to use it, since it is intended for such cases;)

This, however, is not always the best choice. Please look:

.. but I would be surprised if this were a problem for you. Optimization up to this point is very rarely needed.

+4
source

& is bitwise and operator.

This statement simply performs bitwise AND up to 2 sets of bits.

 0 & 0 === 0 1 & 0 === 0 0 & 1 === 0 1 & 1 === 1 

Next to the operator and there is also the operator OR (|) and XOR (exclusive OR) (^).

+3
source

All Articles