This is impossible in C. The ternary operator as you've given it
evaluates either y or z depending on the value of x.  A C function
call always evaluates all of its arguments.  For example.

int w, x = 1, y = 1, z = 1;
w = x ? ++y : ++z;
printf ("%d %d %d\n", y, z);

will print 1 2 1.  If you subsitute,

w = cond(x, ++y, ++z);

for the ternary operator, you'll get 1 2 2.

This may sound like a small thing, but conditional/lazy evaluation is
what leads to the power of Turing completeness in a language.

On Jul 21, 1:29 pm, BALARUKESH <sbalarukesh1...@gmail.com> wrote:
> Ternary operator- x? y : z
> Implement it 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 vars just
> makes things cleaner.

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

Reply via email to