Usage of '&' versus '&&'
&
is a bitwise AND, meaning that it works at the bit level. &&
is a logical AND, meaning that it works at the boolean (true/false) level. Logical AND uses short-circuiting (if the first part is false, there's no use checking the second part) to prevent running excess code, whereas bitwise AND needs to operate on every bit to determine the result.
You should use logical AND (&&
) because that's what you want, whereas &
could potentially do the wrong thing. However, you would need to run the second method separately if you wanted to evaluate its side effects:
var check = CheckSomething();
bool IsValid = isValid && check;
What is the difference between & and && in Java?
& <-- verifies both operands
&& <-- stops evaluating if the first operand evaluates to false since the result will be false
(x != 0) & (1/x > 1)
<-- this means evaluate (x != 0)
then evaluate (1/x > 1)
then do the &. the problem is that for x=0 this will throw an exception.
(x != 0) && (1/x > 1)
<-- this means evaluate (x != 0)
and only if this is true then evaluate (1/x > 1)
so if you have x=0 then this is perfectly safe and won't throw any exception if (x != 0) evaluates to false the whole thing directly evaluates to false without evaluating the (1/x > 1)
.
EDIT:
exprA | exprB
<-- this means evaluate exprA
then evaluate exprB
then do the |
.
exprA || exprB
<-- this means evaluate exprA
and only if this is false
then evaluate exprB
and do the ||
.
Difference between & and && in Java?
&
is bitwise.&&
is logical.
&
evaluates both sides of the operation.&&
evaluates the left side of the operation, if it's true
, it continues and evaluates the right side.
What's the difference between & and && in MATLAB?
The single ampersand & is the logical AND operator. The double ampersand && is again a logical AND operator that employs short-circuiting behaviour. Short-circuiting just means the second operand (right hand side) is evaluated only when the result is not fully determined by the first operand (left hand side)
A & B (A and B are evaluated)
A && B (B is only evaluated if A is true)
What's the difference between & and && in JavaScript?
&
is bitwise AND
This operator is almost never used in JavaScript. Other programming languages (like C and Java) use it for performance reasons or to work with binary data. In JavaScript, it has questionable performance, and we rarely work with binary data.
This operator expects two numbers and retuns a number. In case they are not numbers, they are cast to numbers.
How does it work? Wikipedia has an answer: https://en.wikipedia.org/wiki/Bitwise_operation#AND
&&
is logical AND
Most usually, programmers use this operator to check if both conditions are true, for example:
true && true // returns true
true && false // returns false
However, in JavaScript, it is extended to allow any data type and any number of terms. It returns:
- First term that evaluates to false
- Last term otherwise (if all are true-y)
Here are some examples:
true && false && true // returns false
true && 20 && 0 && false // returns 0 (it is first false-y)
10 && "Rok" && true && 100 // returns 100 (as all are true-y)
&&
short-circuiting
As can be seen from above, as soon as you find one that term is false-y, you needn't to care about the following terms. This allows Javascript to stop evaluation altogether. This is called short circuiting.
This statement doesn't alert anything and false
is returned:
true && false && alert("I am quiet!") // returns false
Therefore, you could use the &&
operator as a shorter replacement for an if statement. These are equivalent:
if (user.isLoggedIn()) alert("Hello!")
user.isLoggedIn() && alert("Hello!")
Almost all JS compressors use this trick to save 2 bytes.
Difference between & and && in PHP
&
is bitwise AND. See Bitwise Operators. Assuming you do 14 & 7
:
14 = 1110
7 = 0111
---------
14 & 7 = 0110 = 6
&&
is logical AND. See Logical Operators. Consider this truth table:
$a $b $a && $b
false false false
false true false
true false false
true true true
Is there any difference between && and & with bool(s)?
The standard guarantees that false
converts to zero and true
converts to one as integers:
4.7 Integral conversions
...
If the destination type is bool, see 4.12. If the source type is bool, the value false is converted to zero and the value true is converted to one.
So the effect in the example you give is guaranteed to be the same and is 100% portable.
For the case you give, any decent compiler is likely to generate identical (optimal) code.
However, for Boolean expressions expr1
and expr2
, it is not true in general that expr1 && expr2
is the same as expr1 & expr2
because &&
performs "short-circuit" evaluation. That is, if expr1
evaluates to false
, expr2
will not even be evaluated. This can affect performance (if expr2
is complicated) and behavior (if expr2
has side-effects). (But note that the &
form can actually be faster if it avoids a conditional branch... Toying with this sort of thing for performance reasons is almost always a bad idea.)
So, for the specific example you give, where you load the values into local variables and then operate on them, the behavior is identical and the performance is very likely to be.
In my opinion, unless you are specifically relying on the "short-circuit" behavior, you should choose the formulation that most clearly expresses your intention. So use &&
for logical AND and &
for bit-twiddling AND, and any experienced C++ programmer will find your code easy to follow.
Related Topics
How to Get More Than 1000 Records from a Directorysearcher
Implementing Zero or One to Zero or One Relationship in Ef Code First by Fluent API
Difference Between Invoke and Dynamicinvoke
Can You Get the Column Names from a SQLdatareader
ASP.NET Core: Exclude or Include Files on Publish
Should C# Have Multiple Inheritance
Entity Framework Very Slow to Load for First Time After Every Compilation
How to Validate Azure Ad Security Token
The Need for Volatile Modifier in Double Checked Locking in .Net
Switch Case: How to Use a Range Instead of a One Number
.Net Core 3.0 Possible Object Cycle Was Detected Which Is Not Supported
Sort Observablecollection<String> Through C#
How to Format a Number into a String with Leading Zeros
Linq Select Distinct with Anonymous Types
Properties VS. Fields: Need Help Grasping the Uses of Properties Over Fields
Deserializing JSON with Unknown Object Names
Why Is .Contains Slow? Most Efficient Way to Get Multiple Entities by Primary Key