tags:

views:

591

answers:

6

Possible Duplicate:
What are bitwise operators?

Recently I came across a few samples that used the | and ^ operator. I am guessing these are or and negation operators.

So what actually do these operators stands for?

+21  A: 
  • | = (bitwise/non-short-circuiting) "or"
  • ^ = "xor"

and for info, "not" (bitwise negation) is ~

Marc Gravell
A: 

^ Logical XOR

| Logical OR

update

in c# its logic operations too:

    Console.WriteLine(true ^ false);  // logical exclusive-or
    Console.WriteLine(false ^ false); // logical exclusive-or
Michael Pakhantsov
-1 Neither of these are logical operators. They're both BitWise...and you mixed XOR and OR.
Justin Niessner
aside from getting XOR and OR backwards the MSDN article listed above from emddudley list them both as Logical
hipplar
Michael, Logical or operator || C/C++, note at bottom of this site(explains difference between logical and bitwise) http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp?topic=/com.ibm.xlcpp8a.doc/language/ref/logore.htm
Michael Eakins
@Meakins: This question is about C#, not C/C++ where you link makes perfectly sense. In C# `|` and `^` can both be logical and bitwise.
0xA3
Michael Eakins
@Meakins, @Justin Niessner: I don't think it is correct to regard bitwise and logical as contrary concepts. In fact they are orthogonal, i.e. *bitwise* actually means that the *logical* operation is applied on bit level. No matter how you see it, it is always a *logical* AND, OR, or XOR operation.
0xA3
I agree that deep down they are almost alike with one exception, The or will happen the same while the and will not as far as returning a boolean is concerned.
Michael Eakins
+7  A: 

MSDN has documentation on all the C# operators at:

http://msdn.microsoft.com/en-us/library/6a71f45d.aspx


EDIT - Jon B commented that a relevant quote from the linked documentation would be useful.

| is the Logical OR 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.

^ is the Logical XOR operator.

Binary ^ operators are predefined for the integral types and bool. For integral types, ^ computes the bitwise exclusive-OR of its operands. For bool operands, ^ computes the logical exclusive-or of its operands; that is, the result is true if and only if exactly one of its operands is true.

emddudley
-1 Posting a link to docs isn't an answer.
Jon B
Or is it... =))
Cipi
@Jon B, so you usually like to repeat things over and over again? Why not use a great resource which explains it instead of re-inventing the wheel?
Filip Ekberg
@Filip - SO is a collection of programming questions and answers meant to outlast other websites and survive link changes on sites like MSDN. Posting a link is fine, but there needs to be a relevant quote
Jon B
Actually it is. Any programmer worth a cent should know how to read langague specifications.
TomTom
Stop()!!! using System.Hammertime.
Cipi
+1 @Flip. SO is meant to contain answers to questions, in the same way Wikipedia contains content. A wikipedia entry that only contains a link to off-site content is not an entry.
Ian Boyd
I think a link to the msdn docs without explanation is fine. Stackoverflow should not become (merely) msdn documentation quotes.
David B
+2  A: 

they are logical bitwise operators

| is a Or link

^ is XOR link

Pharabus
+16  A: 

If you apply it to integral types, they are bitwise or and xor operator. However if you apply them to boolean types, they are logical or and xor. Look at an explanation of or operator and xor operator

You can get more details on boolean operations from the wikipedia truth table

Andrea Parodi
+2  A: 

With integral types, | is a bitwise or, ^ a bitwise xor and for completeness & is a bitwise and.

With boolean types, | is a boolean or, ^ a boolean xor and & a boolean &.

In comparison, || is a short-circuit boolean or - if the first operand evaluates as true the second operand isn't evaluated. && is a short-circuit boolean and - if the first operand is false, the second isn't evaluated. There is no short-circuit ^ because there is no case where the second need not be evaluated.

|| and && are more often used than | and & in boolean cases as there is normally at least a tiny efficiency gain and never a loss. However if the right-hand operand had a side-effect that it was important to trigger in all cases, then | or & would be the one to use. In practice this is rare, and a bad smell (if the side-effect is important, it should be evaluated in a separate expression to make the purpose clearer).

Edit: A source of potential confusion, is that in some other languages integral types can be used as booleans (e.g. you can do if(53) and it's the same as if(true)) this makes the distinctions between the above operators quite different: They're the same if a "purely" boolean type is used (that has only true and false as its possible values) but not otherwise. C# deliberately doesn't allow boolean operations on integral types precisely to prevent the possibilities for mistakes that exist in such languages.

Jon Hanna