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


--- In [email protected], Anup Joshi <joshian...@...> wrote:
>
> Hello friends,
>   I wanted to find biggest number of given two numbers without using the 
> loop statement, the if-conditional, or the ternary operator ?:. One way 
> i could do this was with the following code:
> 
> int biggest(int a, int b) {
>      int result;
> 
>      (a<b) && (result = b); // b is bigger
>      (a>b) && (result =a); // a is bigger
>      (a==b) && (result = a) // both are equal
> 
>      return result;
> }
> 
> this code works for positive as well as negative ints.
> i wanted to know if there was any other way to find the biggest number 
> of the two, by using any other c operator or snippet.
> 
> Thanks & Regards :-)
> 
> anup
>


Reply via email to