Logical and bitwise operators ('&', '^', '|', '&&' and '||')

Previous Binary operators Next

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 valueResults of
  in expr1    in expr2  expr1 & expr2expr1 ^ expr2expr1 | expr2
00000
10011
01011
11101

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.