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.
-
It's not so much for boolean checks as it is for bit flipping, even though it can be used for both. It's a binary AND, not a logical AND. – BoltClock Oct 23 '12 at 06:44
-
I can't understand. What's "single AND operator"? BTW, [`&`](http://msdn.microsoft.com/en-us/library/sbf85k1c(v=vs.80).aspx) is a unary or a binary operator. – Leri Oct 23 '12 at 06:45
-
@PLB: It is both a unary operator and a binary operator. – BoltClock Oct 23 '12 at 06:45
-
@PLB: '&' vs '&&' in an if statement. – ldam Oct 23 '12 at 06:45
-
@BoltClock I've commented quickly. I've added complete information and reference for more info. Thank you, anyway. – Leri Oct 23 '12 at 06:47
-
Basically the same question: [What is the diffference between the | and || or operators?](http://stackoverflow.com/questions/35301/what-is-the-diffference-between-the-and-or-operators) – Paolo Moretti Oct 23 '12 at 06:49
-
@Paolo Moretti: thanks, I must have missed that in my search – ldam Oct 23 '12 at 06:55
6 Answers
&&
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.

- 21,506
- 6
- 72
- 131
It is used for bitwise operation. Refer:
http://www.dotnetperls.com/and
Most common C# bitwise operations on enums
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

- 5,745
- 4
- 30
- 41
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

- 3,451
- 6
- 50
- 86
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:
Both the operand will be calculated.
Operands may not be
bool
always.

- 13,752
- 1
- 36
- 71
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

- 1
- 1

- 9,031
- 5
- 29
- 36