Logical
The following logical operators operate on boolean or integral operands, as noted.
| Sample usage | Read | Type | Explanation |
|---|---|---|---|
| a&b | a bitwise and b | binary | & evaluates both of its operands and returns the logical conjunction (“AND”) of their results. If the operands are integral, the logical conjunction is performed bitwise. |
| a&&b | a and b | binary | && operates on boolean operands only. It evaluates its first operand. If the result is false, it returns false. Otherwise, it evaluates and returns the results of the second operand. Note that, if evaluating the second operand would hypothetically have no side effects, the results are identical to the logical conjunction performed by the & operator. This is an example of Short Circuit Evaluation. |
a | b |
a bitwise or b | binary | | evaluates both of its operands and returns the logical disjunction (“OR”) of their results. If the operands are integral, the logical disjunction is performed bitwise. |
a || b |
a or b | binary | || operates on boolean operands only. It evaluates the first operand. If the result is true, it returns true. Otherwise, it evaluates and returns the results of the second operand. Note that, if evaluating the second operand would hypothetically have no side effects, the results are identical to the logical disjunction performed by the | operator. This is an example of Short Circuit Evaluation. |
| a ^ b | a x-orb | binary | ^ returns the exclusive or (“XOR”) of their results. If the operands are integral, the exclusive or is performed bitwise. |
| !a | not a | unary | ! operates on a boolean operand only. It evaluates its operand and returns the negation (“NOT”) of the result. That is, it returns true if a evaluates to false and it returns false if a evaluates totrue. |
| ~a | bitwise not a | unary | ~ operates on integral operands only. It evaluates its operand and returns the bitwise negation of the result. That is, ~a returns a value where each bit is the negation of the corresponding bit in the result of evaluating a. |