ldam
ldam

Reputation: 4585

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.

Upvotes: 2

Views: 308

Answers (6)

Asik
Asik

Reputation: 22133

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

Upvotes: 3

Prabhu Murthy
Prabhu Murthy

Reputation: 9261

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

Upvotes: 0

Azodious
Azodious

Reputation: 13872

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.

Upvotes: 0

levi
levi

Reputation: 3511

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

Upvotes: 0

Mohnkuchenzentrale
Mohnkuchenzentrale

Reputation: 5885

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

Upvotes: 1

coder_bro
coder_bro

Reputation: 10773

It is used for bitwise operation. Refer:
http://www.dotnetperls.com/and
Most common C# bitwise operations on enums

Upvotes: 1

Related Questions