[Main]
Title=Logical and bitwise operators ('&', '^', '|', '&&' and '||')
Index=&, ^, |, &&, ||

[Top]
The C language offers these bitwise and logical operators:
<PRE>&amp;  (bitwise AND)
^  (bitwise exclusive OR)
|  (bitwise inclusive OR)

&amp;&amp; (logical AND)
|| (logical OR)
</PRE>
They use the following syntax:
<PRE><I>expr1</I> &amp; <I>expr2</I>
<I>expr1</I> ^ <I>expr2</I>
<I>expr1</I> | <I>expr2</I>
<I>expr1</I> &amp;&amp; <I>expr2</I>
<I>expr1</I> || <I>expr2</I>
</PRE>
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 <I>expr1</I> and <I>expr2</I>.
<BR><BR>
For the bitwise operators, each bit in the result is:
<BR><BR>
<TABLE BORDER CELLPADDING="5">
<TR><TD ALIGN="CENTER" COLSPAN="2">Bit value</TD><TD ALIGN="CENTER" COLSPAN="3">Results of</TD></TR>
<TR><TD ALIGN="CENTER">&nbsp;&nbsp;in <I>expr1</I>&nbsp;&nbsp;</TD><TD ALIGN="CENTER">&nbsp;&nbsp;in <I>expr2</I>&nbsp;&nbsp;</TD><TD ALIGN="CENTER"><I>expr1</I> &amp; <I>expr2</I></TD><TD ALIGN="CENTER"><I>expr1</I> ^ <I>expr2</I></TD><TD ALIGN="CENTER"><I>expr1</I> | <I>expr2</I></TD></TR>
<TR><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">0</TD></TR>
<TR><TD ALIGN="CENTER">1</TD><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">1</TD><TD ALIGN="CENTER">1</TD></TR>
<TR><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">1</TD><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">1</TD><TD ALIGN="CENTER">1</TD></TR>
<TR><TD ALIGN="CENTER">1</TD><TD ALIGN="CENTER">1</TD><TD ALIGN="CENTER">1</TD><TD ALIGN="CENTER">0</TD><TD ALIGN="CENTER">1</TD></TR>
</TABLE>
<BR>
Unlike the bitwise operators, <CODE>'&amp;&amp;'</CODE> and <CODE>'||'</CODE> guarantee left-to-right evaluation.
<I>expr1</I> is evaluated first; if it is zero,
<I>expr1</I>&nbsp;&amp;&amp;&nbsp;<I>expr2</I> gives 0 (false), and <I>expr2</I> is
not evaluated at all. With <I>expr1</I>&nbsp;||&nbsp;<I>expr2</I>, if
<I>expr1</I> is nonzero, <I>expr1</I>&nbsp;||&nbsp;<I>expr2</I> gives 1 (true), and
<I>expr2</I> is not evaluated at all.
<BR><BR>
<B>Note:</B> In GNU C the operator '&amp;&amp;' may be also used as unary operator for taking
<A HREF="$$INFOLINK(gnuexts/SEC65)">addresses of labels</A>.
