简体   繁体   English

C#中单个AND运算符的意义是什么?

[英]What is the point of the single AND operator in C#?

Why does & exist? 为什么&存在? My book tells me that & will check for both conditions to be false even if the first one is false, but it is pointless checking if the second condition is false anyway because the first will always make the entire thing false if it is false. 我的书告诉我&即使第一个条件为假,&也会检查两个条件是否都为假,但是无论如何,检查第二个条件是否为假是毫无意义的,因为如果第一个条件为假,则第一个条件总是会使整个事物为假。

&& is the "boolean AND" operator. &&是“布尔AND”运算符。 It evaluates to true if both its operands are true, and false otherwise. 如果两个操作数都为true,则结果为true,否则为false。 It only evaluates the second term if the first is true, because that's a useful optimization. 如果第一个条件为真,则仅评估第二个条件,因为这是有用的优化。

& is the "binary AND" operator. &是“二进制AND”运算符。 It evaluates to the result of applying a bitwise AND operation to its operands. 它评估对其操作数应用按位与运算的结果。 The type of this value is the same as the type of the operands, which can be any integral type or bool. 此值的类型与操作数的类型相同,可以是任何整数类型或布尔值。 It always evaluates both of its operands. 它总是对两个操作数求值。

For boolean operands, the only real practical difference between & and && is that the first always evaluates both operands, while the other performs a short-circuit evaluation . 对于布尔操作数, &&&之间唯一真正的实际区别是,第一个始终评估两个操作数,而另一个则进行短路评估

For integral operands, only the & operator can be used, of course. 当然,对于整数操作数,只能使用&运算符。 Example of a bitwise AND on integers: 整数按位与的示例:

17 & 13 == 1

This is because 17 is 10001, bitwise, 13 is 1101. So the operation is: 这是因为17是10001,按位,13是1101。因此操作是:

   10001
 & 01001
--------
   00001

The same applies to the binary and boolean OR operators ( | and || ). 二进制和布尔OR运算符( ||| )也是如此。

The binary & operator can also function as a unary operator, where it returns the address of the variable it is applied to, as in C. This is can only be used in unsafe code. 二进制&运算符也可以用作一元运算符,在该运算符中它返回要应用的变量的地址,如C中所示。只能在unsafe代码中使用。 Example: 例:

unsafe {
   int a = 3;
   int* addressOfA = &a;
}

Hopefully that clears things up a bit. 希望这可以使事情变得顺利。

No it is not always pointless. 不,并非总是毫无意义的。 Maybe your second check is an Operation that has to be executed even if the first condition ist false. 也许您的第二个检查是即使第一个条件为假也必须执行的操作。

Check this out from MSDN : http://msdn.microsoft.com/de-de/library/sbf85k1c(v=vs.80).aspx 从MSDN进行检查: http : //msdn.microsoft.com/de-de/library/sbf85k1c (v= vs.80).aspx

if(NecessaryFunction() & SecondNecessaryFunction())
{
   // Do something
}

bool NecessaryFunction()
{
   // Do smth useful;
}
bool SecondNecessaryFunction()
{
   // Do smth required and return bool;
}

In this case you want to execute both methods and if one of them is false do not gon inside if 在这种情况下要执行这两种方法,如果其中有一个是假的不要坤内if

but it is pointless checking if the second condition is false anyway because the first will always make the entire thing false if it is false 但是检查第二个条件是否为假是毫无意义的,因为第一个条件如果为假,将始终使整个事物为假

It is true for short-circuit operator: && 对于短路操作员来说确实如此: &&

& : is a bit-wise operator. & :是按位运算符。 with two differences: 有两个区别:

  1. Both the operand will be calculated. 将同时计算两个操作数。

  2. Operands may not be bool always. 操作数不一定总是bool

The & Operator behaves in the following ways &运算符的行为方式如下

When used as a Unary Operator, Returns the Address.But needs to be unsafe. 当用作一元运算符时,返回地址。但是需要不安全。

When used on integers as a Binary Operator, it does a Logical Bitwise AND. 当在整数上用作二进制运算符时,它将执行逻辑按位与。 Example

When used on Bools as a Binary Operator,behaves like a normal &&,but with one difference. 在Bools上用作二元运算符时,其行为与常规&&相似,但有一个区别。 It evaluates the second condition even if the first condition turns out be false. 即使第一个条件为假,它也会评估第二个条件。

num=0;
if(false && num++)       
num will be zero

num=0;    
if(false & num++)    
num will be one

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM