 |
Logical and bitwise operators ('&', '^', '|', '&&' and '||') |
The C language offers these bitwise and logical operators:
& (bitwise AND)
^ (bitwise exclusive OR)
| (bitwise inclusive OR)
&& (logical AND)
|| (logical OR)
They use the following syntax:
expr1 & expr2
expr1 ^ expr2
expr1 | expr2
expr1 && expr2
expr1 || expr2
In first three expressions, both operands must be of integral type.
In fourth and fifth expressions, both operands must be of scalar type.
The usual arithmetical conversions are performed on expr1 and expr2.
For the bitwise operators, each bit in the result is:
Bit value | Results of |
in expr1 | in expr2 | expr1 & expr2 | expr1 ^ expr2 | expr1 | expr2 |
0 | 0 | 0 | 0 | 0 |
1 | 0 | 0 | 1 | 1 |
0 | 1 | 0 | 1 | 1 |
1 | 1 | 1 | 0 | 1 |
Unlike the bitwise operators, '&&'
and '||'
guarantee left-to-right evaluation.
expr1 is evaluated first; if it is zero,
expr1 && expr2 gives 0 (false), and expr2 is
not evaluated at all. With expr1 || expr2, if
expr1 is nonzero, expr1 || expr2 gives 1 (true), and
expr2 is not evaluated at all.
Note: In GNU C the operator '&&' may be also used as unary operator for taking
addresses of labels.