Home >Backend Development >C++ >The difference between | and || in c language
The difference between "|" and "||" in C language lies in the operation type: "|" performs bitwise OR operation, which is true only if both values are true; "||" performs logical OR Operation that is true as long as one or both values are true.
The difference between "|" and "||" in C language
In C language, "|" and "||" are logical operators, which are used to operate on the Boolean value of Boolean expressions.
"|": Bitwise OR operation
The "|" operator performs a bitwise OR operation on two Boolean values:
"||": Logical OR operation
The "||" operator performs a logical OR operation and also operates on two Boolean values:
Summary of differences
Operator | Operation type | Result condition | ||
---|---|---|---|---|
Bitwise or | Both values are true | |||
Logical OR | Either or both values are true |
Example
<code class="c">int a = 1; // 0001 int b = 2; // 0010 int result1 = a | b; // 0011 (按位或) int result2 = a || b; // 1 (逻辑或)</code>
In the first example, the "|" operator performs a bitwise OR operation and the result is "0011". In the second example, the "||" operator performs a logical OR operation and the result is "1" because both values are true.
The above is the detailed content of The difference between | and || in c language. For more information, please follow other related articles on the PHP Chinese website!