Re: [algogeeks] Re: Bitwise Ternary operator

2010-07-25 Thread Terence



On 2010-7-26 1:46, Tech Id wrote:

int cond (int x, int y, int z) {
 return (x==0)*z + (x!=0)*y;
}


Or
 int cond (int x, int y, int z) {
 return (!x) * z + (!!x)*y;
 }
if comparing (==/!=) is not permitted.




On Jul 21, 10:29 pm, BALARUKESH  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.



[algogeeks] Re: Bitwise Ternary operator

2010-07-25 Thread Gene
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  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.



[algogeeks] Re: Bitwise Ternary operator

2010-07-25 Thread Tech Id
int cond (int x, int y, int z) {
return (x==0)*z + (x!=0)*y;
}



On Jul 21, 10:29 pm, BALARUKESH  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.