Home > Backend Development > C++ > body text

The difference between | and || in c language

下次还敢
Release: 2024-05-02 18:15:38
Original
794 people have browsed it

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

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:

  • If both values ​​are true, the result is true.
  • If one or both values ​​are false, the result is false.

"||": Logical OR operation

The "||" operator performs a logical OR operation and also operates on two Boolean values:

  • The result is true if either or both values ​​are true.
  • The result is false only if both values ​​are false.

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>
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template