Home >Backend Development >C++ >The difference between & and && in c++
The difference between
& and && operators is: & is used for bitwise AND operation, comparing the operands bit by bit, and the result is 1 only when both sides are 1 at the same time; while && is used for logical AND operation, checking operation Whether all the numbers are true, as long as one of them is false, the result is false.
The difference between & and &&
In C language, & and && are both operators, but they Has different uses:
1. Bit operator (&):
& operator is used to perform bitwise AND operations, which Meaning it compares the two operands bit by bit and stores the result in the result. Each bit in the result is 1 only if both input bits are 1 at the same time.
For example:
int a = 3; // 二进制为 0011 int b = 5; // 二进制为 0101 int c = a & b; // 按位与运算 // 结果 c 为 0001 (二进制为 1)
2. Logical operator (&&):
&& operator is used to perform logical AND operation, which means it checks whether both operands are true. If both are true, the result is true; otherwise, the result is false.
For example:
bool a = true; bool b = false; bool c = a && b; // 逻辑与运算 // 结果 c 为 false,因为 b 为 false
Summary:
Use | |
---|---|
Bitwise AND operation | |
Logical AND operation |
Note:
The above is the detailed content of The difference between & and && in c++. For more information, please follow other related articles on the PHP Chinese website!