& and && are operators used for logical operations in C language, each with different functions and priorities. & performs a bitwise operation, performing a logical operation on each binary bit to produce a binary result. && performs a logical AND operation, is true only if both operands are true, produces a Boolean result, and has lower precedence than &.
The difference between & and && in C language
& and && are used for logical operations in C language Two operators, but they have different capabilities and precedence.
Bitwise operators (&)
- Perform bitwise operations, that is, perform logical operations on each binary bit of the operand.
- The operation result is a binary number, where each bit is the logical operation result of the corresponding bit of the corresponding operand.
- Commonly used for mask bits, set bits and clear bits.
Logical operator (&&)
- Performs a logical AND operation, that is, the result is only when both operands are true. real.
- The result of the operation is a Boolean value (true or false).
- Short-circuit evaluation, that is, if the first operand is false, the second operand will not be evaluated.
Priority
- & has a higher priority than &&. This means that, without parentheses, the & operation takes precedence over the && operation.
Example
-
& Operation:
- 10 & 5 = 0b1010 & 0b0101 = 0b0000 (4)
-
&& Operation:
- (10 > 5) && (5 != 0) = true && true = true
Conclusion
- & Performs bitwise operations, producing binary results .
- && performs a logical AND operation, producing a Boolean result.
- & has higher priority than &&.
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!
Statement: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