18

Just wondering what the pipe means in this? ive never seen it before:

FileSystemAccessRule fullPermissions = new FileSystemAccessRule(
             "Network Service",
             FileSystemRights.FullControl | FileSystemRights.Modify,
             AccessControlType.Allow);

Cheers

Ian G
  • 29,468
  • 21
  • 78
  • 92
Exitos
  • 29,230
  • 38
  • 123
  • 178

6 Answers6

18

For an enum marked with the [Flags] attribute the vertical bar means 'and', i.e. add the given values together.

Edit: This is a bitwise 'or' (though semantically 'and'), e.g.:

[Flags]
public enum Days
{
     Sunday    = 0x01,
     Monday    = 0x02,
     Tuesday   = 0x04,
     Wednesday = 0x08,
     Thursday  = 0x10,
     Friday    = 0x20,
     Saturday  =  0x40,
}

// equals = 2 + 4 + 8 + 16 + 32 = 62
Days weekdays = Days.Monday | Days.Tuesday | Days.Wednesday | Days.Thursday | Days.Friday;

It's a bitwise-OR but semantically you think of it as an AND!

Jackson Pope
  • 14,520
  • 6
  • 56
  • 80
  • Though semantically correct using the word "and" to describe an "or" to me is extraordinarily confusing. – Joe Apr 18 '11 at 15:34
  • it is, why would I use this to pass into a method? – Exitos Apr 18 '11 at 15:39
  • @Pete2k, if you wanted to use multiple values of a flag-type enum, i.e. in the example in your question, I want FullControl AND Modify. – Jackson Pope Apr 18 '11 at 15:43
  • For those who share confusion with @Joe: with bitwise-OR logic gates, the computer's perspective is : "I want to know if value `x` is true **or** value `y` is true **or** value `z` is true. If it is, I'll evaluate the whole condition to true." When doing things like setting a type attribute to allow multiple types through, we would say `[FooTypeCheck( x | y | z)]`. The computer sees three conditions and only needs one to be valid. The human sees "This member allows type `x` **and** `y` **and** `z`." Thus "semantic and". – Shad Jul 13 '22 at 17:20
11

It is normally a bitwise or operator. In this context, it's used on an enum with the flags attribute set.

recursive
  • 83,943
  • 34
  • 151
  • 241
  • I dont get it so which value are you actually passing to the second parameter? – Exitos Apr 18 '11 at 15:33
  • 1
    You are a passing a combination of `FileSystemRights.FullControl` and `FileSystemRights.Modify`. – recursive Apr 18 '11 at 15:34
  • 10
    Both of them. Think of them as binary: `FileSystemRights.FullControl = 00000001` and `FileSystemRights.Modify = 00000010` The OR combines these fields into `00000011` and passes that value to the method. (These values may not be accurate) – Kyle Trauberman Apr 18 '11 at 15:35
  • Both (FullControl OR FileSystemRights.Modify) – Magnus Apr 18 '11 at 15:35
  • which one though? How can it be a logical OR there is no condition these are hardcoded values in the .net framework.... – Exitos Apr 18 '11 at 15:36
  • why would I do this? im still confused – Exitos Apr 18 '11 at 15:36
  • 2
    Its a Bitwise OR, not a logical OR. – Kyle Trauberman Apr 18 '11 at 15:37
  • I this case it doesn't make much sense because the FullControl would logically override the Modify flag. Consider this though: you want to be able to specifiy read and write permissions. If that enum had a `Read` and a `Write` value, then you would specify both values using the bitwise OR (|). – Kyle Trauberman Apr 18 '11 at 15:38
  • @Kyle: FYI MSDN calls it a logical operator. http://msdn.microsoft.com/en-us/library/6a71f45d(v=VS.80).aspx – recursive Apr 18 '11 at 15:40
  • @Pete2k: There doesn't need to be a condition. `|` is an operator that can be applied to flag enums. The result will be a new value of the same type that isn't necessarily equal to any of the individual enum values, but rather a combination of multiple values. – recursive Apr 18 '11 at 15:43
  • @recursive how would this work in practice? How do I allow eunms in this fasion to be put in the method declaration and then do anything useful with them? – Exitos Apr 18 '11 at 15:47
  • @Pete2K You can read is as "A and B" (i.e., a UNION) but that's not the same as `(A && B)`. The bit is set to `1` if A OR B is `1`. – Mark Cidade Apr 18 '11 at 15:49
  • then in that case its a useless statement. The method wants an enum and we are passing it the value of the logical union of A and B. Not sure how this actually works or is meant to do!? – Exitos Apr 18 '11 at 15:54
  • @Pete2k: It uses the flag attribute. I put a link in my answer. Go read about the flag attribute. – recursive Apr 18 '11 at 16:45
3

It's a bitwise OR of two values, presumably it creates a FileAccessRule with both FullAccess and Modify permissions set.

cbz
  • 1,696
  • 1
  • 15
  • 19
  • 2
    You mean BITWISE or. Logical OR is || – Joe Apr 18 '11 at 15:33
  • +1 FileSystemRights would be an enum that is representative of a bitmask; aka one element of the enum is 1, the next is 2, the next 4, etc. So you can write 1101 as 3 enum values masked. – Tejs Apr 18 '11 at 15:33
  • 1
    @Joe: The c# spec actually calls it a logical operator, even though it makes no sense. – recursive Apr 18 '11 at 15:35
3

It's a binary operator:

Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.

Rhapsody
  • 6,017
  • 2
  • 31
  • 49
  • `+`, `*`, `/`, `%`, `&&`, `||`, and usually `-` are all binary operators. In that context, "binary operator" just means it takes two arguments, one on the left and one on the right. – Anm Apr 18 '11 at 16:37
3

I'm assuming you mean this: FileSystemRights.FullControl | FileSystemRights.Modify

This FileSystemRights, is an enum with FullControl and Modify having their own numeric values.

So if FullControl = 1 and Modify = 2,

FileSystemRights.FullControl | FileSystemRights.Modify = 3.  
00000001 | 00000010 = 00000011.  

Each bit is a "flag" for the method. The input checks to see which "flag" is set and what to do.

So in this example, position 1 (the digit all the way on the right in this case) is FullControl, and position 2 is Modify. The method looks at each of the positions, and changes it behavior. Using flags is a way of passing in multiple parameters of behaviors without having to create a parameter for each possiblity (e.g. bool allowFullControl, bool allowModify) etc.

Bitwise Operator

kemiller2002
  • 113,795
  • 27
  • 197
  • 251
1

It's a boolean or. FullControl and Modify represent bits in a mask. For example 0001 and 0101. If you would combine those via pipe, you would get 0101.

Achim
  • 15,415
  • 15
  • 80
  • 144