how abt this:
(i hv used some variables)

a=x;
b=-x;

k1=a>>7;     (if we assume 8 bit number)
k2=b>>7;

n=k1 | k2;
n=n-1;
k=n>>7;

return (y-k*(y-z));

Explanation:
if(x>0 or x<0)
a and b are of opp signs,
one of k1 and k2 is 0and other is 1...
n=1;
n becomes 0;
k =0;
y is returned....

if(x==0)
a=b=0;
k1=k2=0;
n=0;
n becomes -1
k becomes 1;
z is returned...

correct me if i m wrong...
and it can be reduced in a single statement...

On Sep 12, 10:43 am, teja bala <[email protected]> wrote:
>  If you're familiar with the ? operator x ? y : z
> you have to implement that in a function: int cond(int x, int y, int
> z); using only ~, !, ^, &, +, |, <<, >> no if statements, or loops or
> anything else, just those operators, and the function should correctly
> return y or z based on the value of x. You may use constants, but only
> 8 bit constants. You can cast all you want. You're not supposed to use
> extra variables, but in the end, it won't really matter, using
> variables just makes things cleaner. You should be able to reduce your
> solution to a single line in the end though that requires no extra
> vars.

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to