The &, ^, and | operators are called the logical operators.
and-expression:
equality-expression
and-expression & equality-expression
exclusive-or-expression:
and-expression
exclusive-or-expression ^ and-expression
inclusive-or-expression:
exclusive-or-expression
inclusive-or-expression | exclusive-or-expression
For an operation of the form x op y, where op is one of the logical
operators, overload resolution (?4.2.4) is
applied to select a specific operator implementation. The operands are
converted to the parameter types of
the selected operator, and the type of the result is the return type of the
operator.
The predefined logical operators are described in the following sections.
14.10.1 Integer logical operators
The predefined integer logical operators are:
int operator &(int x, int y);
uint operator &(uint x, uint y);
long operator &(long x, long y);
ulong operator &(ulong x, ulong y);
int operator |(int x, int y);
uint operator |(uint x, uint y);
long operator |(long x, long y);
ulong operator |(ulong x, ulong y);
int operator ^(int x, int y);
uint operator ^(uint x, uint y);
long operator ^(long x, long y);
ulong operator ^(ulong x, ulong y);
The & operator computes the bitwise logical AND of the two operands, the |
operator computes the bitwise
logical OR of the two operands, and the ^ operator computes the bitwise
logical exclusive OR of the two
operands. No overflows are possible from these operations.
14.10.2 Enumeration logical operators
Every enumeration type E implicitly provides the following predefined
logical operators:
E operator &(E x, E y);
E operator |(E x, E y);
E operator ^(E x, E y);
The result of evaluating x op y, where x and y are expressions of an
enumeration type E with an underlying
type U, and op is one of the logical operators, is exactly the same as
evaluating (E)((U)x op (U)y). In other
C# LANGUAGE SPECIFICATION
170
words, the enumeration type logical operators simply perform the logical
operation on the underlying type of
the two operands.
14.10.3 Boolean logical operators
The predefined boolean logical operators are:
bool operator &(bool x, bool y);
bool operator |(bool x, bool y);
bool operator ^(bool x, bool y);
The result of x & y is true if both x and y are true. Otherwise, the result
is false.
The result of x | y is true if either x or y is true. Otherwise, the result
is false.
The result of x ^ y is true if x is true and y is false, or x is false and
y is true. Otherwise, the result
is false. When the operands are of type bool, the ^ operator computes the
same result as the != operator.