6

Possible Duplicate:
What is the diffference between the | and || or operators?
What does | (pipe) mean in c#?

I have some code that was written by another developer in the office, who isn't in at the moment. I have some work to do on his code, but I have not coma across this before. I tried searching on here, but it strips my | out of the search line. I also don't know what the name for that symbol is, so couldn't search for it like that.

this.Action.Values[key] = (int)this.Action.Values[key] | 1;

My question is what does the single or do in this instance?

Community
  • 1
  • 1
Andrew
  • 1,963
  • 3
  • 25
  • 35
  • bitwise 'or' and bitwise 'and' http://en.wikipedia.org/wiki/Bitwise_operation – kenny Mar 16 '12 at 11:57
  • Apologies for the duplicate post, as I mentioned in the question the search box on this site strips out the | symbol. – Andrew Mar 16 '12 at 11:59

9 Answers9

7

The Bar (or pipe), | is a bit-wise OR operator, and the easiest way of explaining it is that it allows us to combine flags. Consider:

[Flags]
public enum WindowFlags
{
    None = 0, 
    Movable = 1,
    HasCloseBox = 2,
    HasMinimizeBox = 4,
    HasMaximizeBox = 8
}

Using the bitwise-OR operator, we can combine flags, thusly:

WindowFlags flags = WindowFlags .Movable | WindowFlags .HasCloseBox | WindowFlags .HasMinimizeBox;

We can "test" for a given flag, using:

bool isMovable = (flags & WindowFlags .Movable);

Removing flags is a bit more of a strain on the eyeballs:

flags &= ~WindowFlags.HasCloseBox;  // remove HasCloseBox flag
Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
4

These are bitwise operations.

Example

  011000101
| 100100100
-----------
= 111100101


  011000101
& 100100100
-----------
= 000000100
bytecode77
  • 14,163
  • 30
  • 110
  • 141
1

This operator mean just OR.

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.

Reference here

See all the operators here in c#

Arion
  • 31,011
  • 10
  • 70
  • 88
1

http://en.wikipedia.org/wiki/Bitwise_operation

& = bitwise AND | = bitwise OR

ChrisB
  • 129
  • 4
1

| --> logical/bitwise OR

& -- > logical/bitwise AND

Code Lღver
  • 15,573
  • 16
  • 56
  • 75
J...
  • 30,968
  • 6
  • 66
  • 143
1

I believe that's a bitwise operator. See http://en.wikipedia.org/wiki/Bitwise_operation.

nootskej
  • 69
  • 7
1

The pipe "|" is a bitwise or operator: http://msdn.microsoft.com/en-us/library/kxszd0kx.aspx

David Brabant
  • 41,623
  • 16
  • 83
  • 111
1

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.

http://msdn.microsoft.com/en-us/library/kxszd0kx(v=vs.100).aspx

Matus Nemcik
  • 240
  • 2
  • 13
1

A single | is bitwise OR operator

ionden
  • 12,536
  • 1
  • 45
  • 37