On 12/27/2009 2:01 AM, py2akv wrote:
>
> Anup,
>
> The code of my friend has superfluous statements.
>
> Try this, shorter and faster:
>
> int greater(int a, int b) {
> int result=b; // (1)
> (a>b) && (result=a); // (2)
> return result;
> }
>
> (1) I suppose it's b.
> (2) If a>b, the testing cannot be logically predicted yet, is not
> short-circuited and result is a; otherwise (if not a>b), the testing
> can already be logically predicted (with &&, one negative is enough),
> is short-circuited and result is already known.
>
> And to work out the lesser:
>
> int lesser(int a, int b) {
> int result=b; // (1)
> (a>b) || (result=a); // (2)
> return result;
> }
>
> (1) I suppose it's b.
> (2) If a>b, the testing can already be logically predicted (with ||,
> one positive is enough), is short-circuited and result is already
> known; otherwise (if not a>b), the testing cannot be logically
> predicted yet, is not short-circuited and result is a;
>
> Good results!
>
> Geraldo
>
could you please spare some time to explain the (2)'s of both the
snippet, unfortunately i could not understand the testing thing :-[
Regards
anup
[Non-text portions of this message have been removed]