简体   繁体   中英

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. It evaluates to true if both its operands are true, and false otherwise. It only evaluates the second term if the first is true, because that's a useful optimization.

& is the "binary AND" operator. 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:

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

The same applies to the binary and boolean OR operators ( | and || ).

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. 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

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

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.

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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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