The difference between (&&, ||) and (&, |) in C#

Preface

In the C# programming language, logical operators are used to combine and compare conditions to control the flow and behavior of a program. Among logical operators, there are two very important pairs of operators: && and ||, & and |. Although they look similar, they actually have different behaviors and usage scenarios. Below we will introduce the differences and usage of these two pairs of operators in detail.Insert image description here



&& and || operators

1. Logical AND operator (&&)

The logical AND operator (&&) is used to determine whether multiple conditions are true. When all conditions are true, the result is true; as long as one condition is not true, the result is false. Examples of usage of this operator are as follows:

bool condition1 = true;
bool condition2 = false;

if (condition1 && condition2)
{
    
    
    Console.WriteLine("条件都为真");
}
else
{
    
    
    Console.WriteLine("至少有一个条件为假");
}

In the above example, since condition2 is false, the final result is "at least one condition is false".

2. Logical OR operator (||)

The logical OR operator (||) is used to determine whether at least one of multiple conditions is true. The result is true when any one condition is true; the result is false only when all conditions are false. Examples of usage of this operator are as follows:

bool condition1 = true;
bool condition2 = false;

if (condition1 || condition2)
{
    
    
    Console.WriteLine("至少有一个条件为真");
}
else
{
    
    
    Console.WriteLine("所有条件都为假");
}

In the above example, since condition1 is true, the final result is "at least one condition is true".

When using && and ||, you need to pay attention to the following points:

  • Short-circuit evaluation: When using && and ||, if the first condition can already determine the final result, then the judgment of subsequent conditions will be skipped. For example, when the conditional expression is false && func(), func() will not be called, because no matter what the return value of func() is, the result is false. When the condition is true || func(), func() will not be called, because no matter what the return value of func() is, the result is true.

  • Number of operations: When using && and || to make logical judgments, you only need to perform necessary operations instead of making complete judgments on all conditions. This can improve program performance in some cases.


& and | operators

Insert image description here

1. Bitwise AND operator (&)

The bitwise AND operator (&) is used to perform a bitwise AND operation on two integers. It compares the binary representations of two integers and performs a logical AND operation on the bits at the corresponding positions. Examples of usage of this operator are as follows:

int number1 = 5;  // 二进制表示为 0000 0101
int number2 = 3;  // 二进制表示为 0000 0011

int result = number1 & number2;  // 二进制表示为 0000 0001

Console.WriteLine(result);  // 输出 1

In the above example, the value of the result variable is 1, which represents the result of the bitwise AND operation.

2. Bitwise OR operator (|)

The bitwise OR operator (|) is used to perform a bitwise OR operation on two integers. It compares the binary representations of two integers and logically ORs the bits at the corresponding positions. Examples of usage of this operator are as follows:

int number1 = 5;  // 二进制表示为 0000 0101
int number2 = 3;  // 二进制表示为 0000 0011

int result = number1 | number2;  // 二进制表示为 0000 0111

Console.WriteLine(result);  // 输出 7

In the above example, the value of the result variable is 7, which represents the result of the bitwise OR operation.

When using & and |, you need to pay attention to the following points:

  • Operation rules: Use & to perform a bitwise AND operation on two integers. Only when both bits at the corresponding position are 1, the result bit will be 1; use | to perform a bitwise OR operation on two integers, as long as the corresponding position If one of the two bits on is 1, the result bit is 1.

  • Evaluation of all bits: When using & and | to perform bit operations, all bits will be operated on, and there is no short-circuit evaluation feature. This means that a complete comparison will be made for subsequent conditions regardless of the preceding condition.


To sum up, && and || are logical operators, used for combination and judgment of conditions; & and | are bit operators, used for bitwise operations on integers. In actual programming, we should choose appropriate operators to complete logical operations according to specific situations. It should be noted that when using bit operators, you must clearly understand the rules and results of bit operations to avoid erroneous logical judgments.

I hope this article can provide a more detailed understanding of the differences between &&, ||, &, and | in C#, and enable the correct use of these operators to control the flow and behavior of the program.

Guess you like

Origin blog.csdn.net/qq_22120623/article/details/135083123