X hits on this document

1456 views

0 shares

168 / 396

Chapter ‎18   Unsafe code

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.

### 7.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 words, the enumeration type logical operators simply perform the logical operation on the underlying type of the two operands.

### 7.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.

## 7.11 Conditional logical operators

The && and || operators are called the conditional logical operators. They are also called the “short-circuiting” logical operators.

conditional-and-expression: inclusive-or-expression conditional-and-expression   &&   inclusive-or-expression

conditional-or-expression: conditional-and-expression conditional-or-expression   ||   conditional-and-expression

The && and || operators are conditional versions of the & and | operators:

The operation x && y corresponds to the operation x & y, except that y is evaluated only if x is true.

The operation x || y corresponds to the operation x | y, except that y is evaluated only if x is false.

An operation of the form x && y or x || y is processed by applying overload resolution (§‎7.2.4) as if the operation was written x & y or x | y. Then,

If overload resolution fails to find a single best operator, or if overload resolution selects one of the predefined integer logical operators, a compile-time error occurs.

Otherwise, if the selected operator is one of the predefined boolean logical operators (§‎7.10.2), the operation is processed as described in §‎7.11.1.