By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 867 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

Implementation of conditional operator

P: n/a
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.
K2G

Dec 12 '05 #1
Share this Question
Share on Google+
14 Replies


P: n/a


gu**********@gmail.com wrote On 12/12/05 11:04,:
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.


The best answer is `x?y:z'. Why would you want
any other?

--
Er*********@sun.com

Dec 12 '05 #2

P: n/a
I would like to know is there any such thing available in C by whcih
without using ? or if , we can take decision. I know using ? of if is
the best way but still just to know whether there is any other way of
taking decision based on some operator :)

Dec 12 '05 #3

P: n/a
Hi

Keshav wrote:
I would like to know is there any such thing available in C by whcih
without using ? or if , we can take decision. I know using ? of if is
the best way but still just to know whether there is any other way of
taking decision based on some operator :)


Yes, and it's quite easy. Anyway, I hope you're only asking because this is
an assignment or a fun question...

Hint: What operators do you know that do not necessarily evaluate all
parameters?

(I just realize: Are you "allowed" to use , ?)

Markus

Dec 12 '05 #4

P: n/a

Keshav wrote:
I would like to know is there any such thing available in C by whcih
without using ? or if , we can take decision. I know using ? of if is
the best way but still just to know whether there is any other way of
taking decision based on some operator :)


Sounds like homework. Perhaps you should try doing it? Or you
could post the name of your professor, we could then cut out
the middleman and directly send our answers to get credit.

If you are writing programs in C, you have access to ? and if, and
there is no reason not to use them.

-David

Dec 12 '05 #5

P: n/a
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

gu**********@gmail.com wrote:
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.


Since the C language includes the trinary operator, if() statements, and loops,
and you can't have C without them, there's no obvious reason that you would need
to answer this sort of question. Just use the trinary operator, or an if()
statement.

OTOH, if this is /homework/, intended to exercise your knowledge of C, then the
artificial constraints on this problem are acceptable. What would /not/ be
acceptable, however, is for us to do your homework for you.

I'll give you a hint: For a limited subset of types for a, x, y, z
the expression
a = x ? y : z;
/can/ be rewritten within your stated constraints. Think logically, and limit
your answer to a suitable subset of types, and you'll be able to figure it out
on your own.

- --
Lew Pitcher
IT Specialist, Enterprise Data Systems,
Enterprise Technology Solutions, TD Bank Financial Group

(Opinions expressed are my own, not my employers')
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (MingW32)

iD8DBQFDnaPbagVFX4UWr64RAtrZAKCYjPdgJ2QunwVGC/vkYxIJVbZ93ACfSwsy
uXtEJRFSvGPUh4S2oXOGMKQ=
=zAuG
-----END PGP SIGNATURE-----
Dec 12 '05 #6

P: n/a
gu**********@gmail.com wrote:
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.


I have almost everything I need, but could you also give me the address of
your class instructor or prospective employer? I can send the answer
directly that way.

The question is flawed, by the way. It's impossible to implement the ternary
operator with the symbols given if you don't restrict the question some more
(e.g., only 'int' expressions).

When you've solved it, it's instructive to solve it again with the
constraint that none of the expressions should be evaluated more than once,
if this isn't already so.

S.
Dec 12 '05 #7

P: n/a


Keshav wrote On 12/12/05 11:18,:
I would like to know is there any such thing available in C by whcih
without using ? or if , we can take decision. I know using ? of if is
the best way but still just to know whether there is any other way of
taking decision based on some operator :)


(Please quote enough context in a reply to allow a
reader to understand what you're talking about without
reading all the preceding messages. For the benefit of
latecomers, Keshav has desires to produce the same effect
as `x?y:z' without using the conditional operator, an `if'
statement, or any loops. He wishes to restrict himself
to the symbols ~, !, ^, &, +, |, <<, and >>.)

So: Is there a way to fulfil Keshav's silly desire?
I believe the answer is "No, not for general y and z."

--
Er*********@sun.com

Dec 12 '05 #8

P: n/a

gu**********@gmail.com wrote:
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.
K2G


y & ~x + x + !x | z & ~x + x + !!x;

Dec 13 '05 #9

P: n/a
On 2005-12-12, Eric Sosman <er*********@sun.com> wrote:


gu**********@gmail.com wrote On 12/12/05 11:04,:
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.
are || and && permitted? [these are distinct symbols from & and |]

if so,

(x&&(y||1))||z will have the same effect [if x, then y, else z] - it
won't return the same as "x?y:z" though.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.


The best answer is `x?y:z'. Why would you want
any other?

Dec 13 '05 #10

P: n/a
"us******@gmail.com" <us******@gmail.com> writes:
gu**********@gmail.com wrote:
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.
K2G


y & ~x + x + !x | z & ~x + x + !!x;


Not working:

#include <stdio.h>

int main(void)
{
const char* x = NULL;
const char* y = "hello";
const char* z = "good bye";

printf("%s\n", x?y:z);
printf("%s\n", y & ~x + x + !x | z & ~x + x + !!x);
return 0;
}

/Niklas Norrthon
Dec 13 '05 #11

P: n/a

"Niklas Norrthon" <do********@invalid.net> wrote in message
news:0p************@niklas.ua.dynas.se...
"us******@gmail.com" <us******@gmail.com> writes:
gu**********@gmail.com wrote:
> Hi,
> I want to know the trick of implementing condition operator or
> (if()... else...).
>
> Symbols to use:
> ~, !, ^,&, +, |, <<, >>.
>
> NOt to use:
> if statement,
> loops,
>
> and it should give same result as x?y:z gives.
> Thanks in advance.
> K2G


y & ~x + x + !x | z & ~x + x + !!x;


Not working:

#include <stdio.h>

int main(void)
{
const char* x = NULL;
const char* y = "hello";
const char* z = "good bye";

printf("%s\n", x?y:z);
printf("%s\n", y & ~x + x + !x | z & ~x + x + !!x);
return 0;
}


Yet it works ok on ints ...

BTW

I hand parsed the original expression for precedence and associativity - had
to, in order to [try and ] understand it!

This is what I came up with ...

#define F(x,y,z) ((y) & ((~x) + ((x) + (!x)))) | ((z) & ((~x) + ((x) +
(!!x))))

It seems to *still* work, so I assume I got it right. However, I was
wondering whether there are any simple parsers that would take a complex
expression like (the original) y & ~x + x + !x | z & ~x + x + !!x; and
paren it accordingly? Better still, something that can break it down into
simpler sub expressions, or a tree representation, e.g. ...

a = ((y) & ((~x) + ((x) + (!x))));

b = ((z) & ((~x) + ((x) + (!!x))));

c = a | b;
Dec 14 '05 #12

P: n/a
"rayw" <ra*********@gmail.com> writes:
"Niklas Norrthon" <do********@invalid.net> wrote in message
news:0p************@niklas.ua.dynas.se...
"us******@gmail.com" <us******@gmail.com> writes:
gu**********@gmail.com wrote:
> Hi,
> I want to know the trick of implementing condition operator or
> (if()... else...).
>
> Symbols to use:
> ~, !, ^,&, +, |, <<, >>.
>
> NOt to use:
> if statement,
> loops,
>
> and it should give same result as x?y:z gives.
> Thanks in advance.
> K2G

y & ~x + x + !x | z & ~x + x + !!x;


Not working:

#include <stdio.h>

int main(void)
{
const char* x = NULL;
const char* y = "hello";
const char* z = "good bye";

printf("%s\n", x?y:z);
printf("%s\n", y & ~x + x + !x | z & ~x + x + !!x);
return 0;
}


Yet it works ok on ints ...


x?y:z works with ints *and* pointers.

This is similar to the old swap hack:

void swap(int* x, int* y)
{
*x ^= *y;
*y ^= *x;
*x ^= *y;
}

Seems to work, but try:

int a = 7;
swap(&a, &a);

/Niklas Norrthon
Dec 14 '05 #13

P: n/a
gu**********@gmail.com wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.


Why do professors continue to ask these silly homework problems?
Are they really that strapped for good ideas that might
actually teach something useful?

I'm genuinely intested if anyone sees any actual teaching value
to exercises of this nature for C students.

--
Randy Howard (2reply remove FOOBAR)
"The power of accurate observation is called cynicism by those
who have not got it." - George Bernard Shaw

Dec 14 '05 #14

P: n/a
Randy Howard <ra*********@FOOverizonBAR.net> writes:
gu**********@gmail.com wrote
(in article
<11**********************@g43g2000cwa.googlegroups .com>):
Hi,
I want to know the trick of implementing condition operator or
(if()... else...).

Symbols to use:
~, !, ^,&, +, |, <<, >>.

NOt to use:
if statement,
loops,

and it should give same result as x?y:z gives.
Thanks in advance.


Why do professors continue to ask these silly homework problems?
Are they really that strapped for good ideas that might
actually teach something useful?

I'm genuinely intested if anyone sees any actual teaching value
to exercises of this nature for C students.


There is some danger in these "How can you drive a nail without using
a hammer?" questions, in that they can encourage students to think
that excessively clever solutions are a good idea.

On the other hand, they can probably be a decent way for students to
demonstrate an ability to construct complex solutions to problems, a
skill that's important in the real world.

The canonical first C program just prints "Hello, world". Nobody
really needs a program that just prints "Hello, world" (on most
systems the "echo" command is simpler and more flexible). The point
is to demonstrate the ability to write a program. Similarly, nobody
*needs* to compute the size of an object without using "sizeof", or
implement a conditional operator using an artificially restricted set
of operations, but if you *can* do so, it probably implies some
understanding of the language.

The instructor's problem is to create an exercise that can be stated
briefly and unambiguously, but that requires some significant amount
of knowledge and work.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 14 '05 #15

This discussion thread is closed

Replies have been disabled for this discussion.