Difference Between | and || , or & and &&

Difference between | and || , or & and &&

The operators |, &, and ~ act on individual bits in parallel. They can be used only on integer types. a | b does an independent OR operation of each bit of a with the corresponding bit of b to generate that bit of the result.

The operators ||, &&, and ! act on each entire operand as a single true/false value. Any data type can be used that implicitly converts to bool. Many data types, including float implicitly convert to bool with an implied !=0 operation.

|| and && also "short circuit". That means whenever the value of the result can be determined by just the first operand, the second is not evaluated. Example:

ptr && (*ptr==7) If ptr is zero, the result is false without any risk of seg faulting by dereferencing zero.

You could contrast that with (int)ptr & (*ptr). Ignoring the fact that this would be a bizarre operation to even want, if (int)ptr were zero, the entire result would be zero, so a human might think you don't need the second operand in that case. But the program will likely compute both anyway.

What is the difference and what are the & and && operators used for?

The key difference is that the & operator is a bitwise operator, while the && operator is a logical operator.

Bitwise operators work on bits and perform "bit by bit" operations, they are applied to the bits of one or two operands. The & represents a bitwise AND operation- where A and B represent two inputs; both inputs must be true in order for C to be true.

Sample Image

So for instance in the example you provided you have:

if(0 & 1){
}

The result of the bitwise AND on 0 and 1 as inputs is 0 (false) because both inputs must be true for the output to be true.

The && operator is a logical operator, which is used to make a decision based on multiple conditions. It can apply to one or two operands each of which may be true or false.

if (!a && b < 5) {
}

The above still evaluates to false because both conditions must be met for the code inside the if statement to be executed. In addition to this, using the && operator will (depending on the language) short circuit, which means that the second condition of the if will only be evaluated if the outcome is not determined by the first condition.

So this expression will fail as soon as a is found to be zero (false) because there is no point in evaluating the second expression as the first expression had to be true. This is another difference as the bitwise & operator does not short circuit.

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.

Difference between | and || or & and && for comparison

in C (and other languages probably) a single | or & is a bitwise comparison.

The double || or && is a logical comparison.

Edit: Be sure to read Mehrdad's comment below regarding "without short-circuiting"

In practice, since true is often equivalent to 1 and false is often equivalent to 0, the bitwise comparisons can sometimes be valid and return exactly the same result.

There was once a mission critical software component I ran a static code analyzer on and it pointed out that a bitwise comparison was being used where a logical comparison should have been. Since it was written in C and due to the arrangement of logical comparisons, the software worked just fine with either. Example:

if ( (altitide > 10000) & (knots > 100) )
...

What is the difference between && and ||?

You are looking at a "C++ puzzle" which is using two languages tricks.

The first is that postincrement uses the value of the variable first, then increments the variable.

So x++ has the value 2 in the expression and then x becomes 3
y++ has the value 0 in the expression, and then y becomes 1

&& is and
|| is or

both operators are short-circuiting.

Look at && first.

In order for and to be true, both sides must be true (non-zero)

Since it is x++ && y++, first the x++ happens, and since it is non-zero (true)
the y++ has to happen to determine whether the result is true or not.
Therefore x is 3 and y is 1.

The second case is the same. y is zero, but in order to determine if the OR is true,
the if statement executes the second half of the expression.

The third case, since it is in the opposite order, never executes y++ because

x++ || y++

the first half being x++, it is already true, so the compiler does not bother to execute the second half of the test.



Related Topics



Leave a reply



Submit