R
Richard Harter
Boltar said:
That's the kind of thing a good optimising compiler will already do.
Even though the RHS might have side effects?
Boltar said:
That's the kind of thing a good optimising compiler will already do.
No, they are not.
An important goal of C was to generate efficient code. The operands of the
logical operators &&, ||, !, and ? must usually be tested at run time for a
true or false value, normally by a conditional branch. There is no extra
cost to short circuit evaluation and it is a short-hand convenience to
programming.
Richard said:Boltar said:
That's the kind of thing a good optimising compiler will already do.
Right. But theres no point in checking the LHS side first for a 2 inBoltar said:Yes , if the LHS value is 0xFFFFFFF or whatever theres no point
evaluating the RHS of a bitwise OR.
Boltar said:Why does C do lazy evaluation for logical boolean operations but not
bitwise ones?
Ie: the following program prints "1 2" , not "1 1" under gcc
main()
{
int a = 1;
int b = 1;
0 && ++a;
0 & ++b;
printf("%d %d\n",a,b);
}
Surely the bitwise boolean ops are just as appropriate for doing lazy
evaluation with?
Even though the RHS might have side effects?
Really?
Methinks, & operator always evaluate both operands, just like the |
operator.
Richard said:Tor Rustad said:
I was thinking of cases such as 0 & 1, 0 & vanilla_int_object, and so on.
The context was:
0 && ++a;
0 & ++b;
Richard said:This isn't really right.
The point of short circuit evaluation
of && and || is to suppress evaluation of the RHS when the LHS
establishes the truth of falsity of the boolean expression. This
is a programming convenience in code patterns like
if (ptr && foo(ptr)) {...}
Boltar said:Why? If theres a zero on the LHS of a bitwise AND you know the result
will be zero no matter what is on the RHS so why bother evaluating the
RHS?
By that reasoning, a * x++ shouldn't increment x when a is 0, either. Or
foo < bar() shouldn't call bar if it returns an int and foo is INT_MIN.
If foo == INT_MIN, the result of foo < bar() depends on whether bar also
returns INT_MIN. If foo == INT_MAX, the result doesn't depend on bar.
#include <stdio.h>
int bar(void) { return 0; }
int foo = INT_MAX;
int main(void) {
printf("%d\n", foo < -bar());
return 0;
}
-0 is valid but i think -- not sure at all -- that -x where x is 0No, that's not allowed. -0 is always equal to plain old zero. I
understand the point you're making, and it is a valid one, but you
would've needed to use ~bar() as an example.
As long as 'x' is of type int, short or signed char yes, but now IHow about "If the evaluation of x is defined to produce a result, then
the value of INT_MAX < x does not depend on the value of x."? It also
covers the case where the evaluation of x leads to a call to exit() or
abort().
-0 is valid but i think -- not sure at all -- that -x where x is 0 might
be a trap representation..
Maybe someone else knows?
As long as 'x' is of type int, short or signed char yes, but now I have
lost your point..
-0 is valid but i think -- not sure at all -- that -x where x is 0
might be a trap representation..
Maybe someone else knows?
^^^ haveBen Bacarisse said:So for arithmetic operations like -, you can to have a negative zero
to start with.
Tor Rustad said:Many C coding standards prohibits side-effects on the second operand of
the && and the || operators, because there are some clueless
programmers, not aware of the conditional evaluation of the second operand.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.