I am (still) learning C# - and I thought I understood the difference between &
& &&
as well as |
& ||
Bitwise operations are used on integers. You must view the entire integer as 32 individual bits. Most .NET developers rarely use bitwise operations. Check out wikipedia for more clarification.
The difference will be less apparent in booleans; bitwise operators are primarily for numbers, whereas logical operators are primarily for booleans. In a bitwise operation (e.g., &), the operation is performed for each bit. In a logical operation (e.g., &&), the operation is performed for the entire result.
For example, the bitwise & of 11 (1011 in binary) and 2 (10 in binary) would be computed as such:
1011
& 0010
______
0010
which is 2.
There is an additional consideration in how the two types of operators are executed. When using a bitwise operator, the expressions on either side of the operator are first executed, and then the operation is performed. When using a logical operator, however, the expression on the left side of the operator is performed first, and the right side may be neglected if it will not change the result.
For example, when I execute (false expression) && (true expression)
, the true expression is never evaluated. Likewise with (true expression) || (false expression)
. This is referred to as short-circuit evaluation
I think you are getting tripped up because C# has overloaded |
and &
. Used with numeric primitives then they are bitwise operations. Used with booleans then they are just like ||
and &&
except they don't short circuit.
For example
bool Foo() {
return false;
}
bool Bar() {
return true;
}
if(Foo() & Bar()) {
// do something
}
// versus
if(Foo() && Bar()) {
// do something 2
}
In the above example, the first boolean expression will execute both Foo() and Bar(), but in the second one only Foo() will execute.
IMO this is one of the worst decisions the C# team has made. It leads to confusing and occasionally subtle errors.
When using booleans there is no difference. The difference becomes more noticable when writing
1 & 2 = 3
1 && 2 = Error // Not 'true' as I wrote before. It would be in C++, but not in C#.
The double operator is a logical operator. It will treat each side as a boolean and returns a boolean that tells you something about these values (operands).
The single operator is a bitwise operator. It takes all bits in each value, performs the boolean logic on those bits, and combines the resulting bits into a new value.
Because a boolean value contains only one bit, the result for boolean operands is the same when using single (bitwise) or double (logical) operator, but since you're (probably) doing some logical operations, it's logical to use the logical operator too. (no pun intended)
&& and || are used with booleans. Your output makes sense when using these.
& and | are bitwise operator, meaning they are applied to the operands bit by bit. For example
110010010 |
001000100 =
111010110
using the same table of your program's output but a bit a time. They are mainly used with integers, not booleans.
you should read this Short-circuit evaluation.