Re: [algogeeks] Re: C Trick

2011-08-26 Thread rajesh singarapu
abs function itself has a condition?? I think it is not that good solution. On Wed, Aug 24, 2011 at 6:33 PM, priyanka raju priyark...@gmail.com wrote: int a,b,max,min; max=(a+b+abs(a-b))/2; min=(a+b-abs(a-b))/2; -- cheers priyanka -- You received this message because you are

[algogeeks] Re: C Trick

2011-08-26 Thread Dave
@Rajesh: abs() can be done without conditional operations. There probably are many ways. The first two that come to mind are: abs(x) = (x 31) x | ~(x 31) ~x abs(x) = (x 31) ^ x + (x 31) 1 Dave On Aug 26, 2:56 am, rajesh singarapu rajesh0...@gmail.com wrote: abs function itself has a

Re: [algogeeks] Re: C Trick

2011-08-24 Thread priyanka raju
int a,b,max,min; max=(a+b+abs(a-b))/2; min=(a+b-abs(a-b))/2; -- cheers priyanka -- You received this message because you are subscribed to the Google Groups Algorithm Geeks group. To post to this group, send email to algogeeks@googlegroups.com. To unsubscribe from this group, send email to

Re: [algogeeks] Re: C Trick

2011-08-24 Thread Sanjay Rajpal
nice solution priyanka :) Sanju :) On Wed, Aug 24, 2011 at 6:03 AM, priyanka raju priyark...@gmail.com wrote: int a,b,max,min; max=(a+b+abs(a-b))/2; min=(a+b-abs(a-b))/2; -- cheers priyanka -- You received this message because you are subscribed to the Google Groups Algorithm

[algogeeks] Re: C Trick

2011-08-23 Thread Dave
@Abhishek: int c = (a - b) 31; max = c b | ~c a; Explanation: c = 0 if a = b, else c = all one bits. Then c b = 0 if c = 0, but c b = b if c = all ones, i.e. if the max is b, and ~c a = a if c = 0, i.e., if the max is a, but ~c a = 0 if c = all ones. Dave On Aug 23, 8:07 am, Abhishek