I am wondering if this two approaches are equivalent? Is one of them better than the other?
First:
bool x = foo();
bool y = bar();
if(x || y)
{
//...
}
Second:
if(foo() | bar())
{
//...
}
I am wondering if this two approaches are equivalent? Is one of them better than the other?
First:
bool x = foo();
bool y = bar();
if(x || y)
{
//...
}
Second:
if(foo() | bar())
{
//...
}
http://msdn.microsoft.com/en-us/library/6373h346(v=vs.71).aspx
The conditional-OR operator (||) performs a logical-OR of its bool operands, but only evaluates its second operand if necessary.
http://msdn.microsoft.com/en-us/library/kxszd0kx(v=vs.71).aspx
Binary | operators are predefined for the integral types and bool. For integral types, | computes the bitwise OR of its operands. For bool operands, | computes the logical OR of its operands; that is, the result is false if and only if both its operands are false.
These two versions are equivalent. The ||
operator short-circuits, but |
does not. Since you evaluate x
and y
before testing x || y
, that comes to the same as foo() | bar()
.
Now, if you had compared
foo() | bar()
with
foo() || bar()
then they would be different. However, your prior evaluation into x
and y
was sufficient to render the two tests equivalent.
The main difference between |
and ||
is short circuiting, but that doesn't matter here, since your code that uses ||
already evalates both expressions beforehand, short circuiting is irrelevant here.
Assuming both foo()
and bar()
return a normal bool, this should be equivalent.
If the return type is different from bool
implicit conversions and overload resolution can lead to different results.
If you use hacked bools(AFAIK only achievable with unsafe code) which have an underlying value different from 0
or 1
they can be different too.
With hacked bools we can observe that |
has binary semantics, even on bools. I'm not sure if this is specified behavior at the runtime level, or if it's just undefined behavior, and as such might change with different compiler/jitter versions. My implementation with explicit struct layout is certainly undefined behavior, but I'm not sure if other languages can create such bools without such a hack.
void Main()
{
bool b1 = hackbool(1);
bool b2 = hackbool(2);
((b1 | b2)&b2).Dump();
((b1 || b2)&b2).Dump();
}
[StructLayout(LayoutKind.Explicit)]
internal struct Evil
{
[FieldOffset(0)]public int i;
[FieldOffset(0)]public bool b;
}
bool hackbool(int i)
{
Evil e=new Evil();
e.i=i;
return e.b;
}
if(foo() | bar())
{
//...
}
If you meant foo() || bar()
, then, this will be faster than first version in one case. When foo()
is true, it won't execute bar()
But in case of |, it is equivalent
||
is a conditional-OR (tests boolean values), and |
is a bitwise-OR.
| is an artihmetic operator. so (in binary) 0101 | 0111 = 0111 (you put a one where either operand has a 1..
|| is a logical operator, if (a||b) { do something } occurs if a or b is true.
They are totally different.
|| is also a 'sequence point.' It is possible, for example, that if you coded
if (foo() || bar())
....
If foo() returned true, bar() might not even be called because 'if' has all it needs to evalaute teh expression as true. That may or may not be good!