in C (and other languages probably) a single |
or &
is a bitwise comparison.
The double ||
or &&
is a logical comparison.
Edit: Be sure to read Mehrdad's comment below regarding "without short-circuiting"
In practice, since true
is often equivalent to 1
and false
is often equivalent to 0
, the bitwise comparisons can sometimes be valid and return exactly the same result.
There was once a mission critical software component I ran a static code analyzer on and it pointed out that a bitwise comparison was being used where a logical comparison should have been. Since it was written in C and due to the arrangement of logical comparisons, the software worked just fine with either. Example:
if ( (altitide > 10000) & (knots > 100) )
...
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…