If number is 0 return zero else return one

L

Lakshmi Sreekanth

Hi,

My problem: I need to return 0 if number = 0 other wise return value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Waiting for clue.

Regards,
Laks ..... Sree.....
 
N

Noob

Lakshmi said:
My problem: I need to return 0 if number = 0 other wise return value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Hint: the "!" operator maps 0 to 1 and everything else to 0.
 
R

Rich Webb

Hi,

My problem: I need to return 0 if number = 0 other wise return value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Waiting for clue.

Can do. However, please provide your teacher's email address so that we
can send him/her the answer directly. Much more efficient ...
 
N

Nick Keighley

Lakshmi Sreekanth wrote:



Hint: the "!" operator maps 0 to 1 and everything else to 0.

run this

#include <stdio.h>

int main (void)
{
printf ("%d %d\n", 1 == 0, 0 == 0);
return 0;
}

does that give a hint?
 
B

Ben Bacarisse

Lakshmi Sreekanth said:
My problem: I need to return 0 if number = 0 other wise return value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Waiting for clue.

You have only one object to consider (number) and 9 operators (+ is
two, yes?) so you can get the answer by listing all the possibilities
in some logical order. I suggest some sort of "increasing
complexity":

No operators:
number

1 operator:
!number
~number
+number
number & number
number ^ number
number | number
number + number
number << number
number >> number

2 operators:
!!number
!~number
!+number
~!number
~~number
~+number
+!number
+~number
++number
!number & number
!number ^ number
!number | number
!number + number
!number << number
!number >> number
number & !number
number ^ !number
number | !number
number + !number
number << !number
number >> !number
...
number >> +number

3 operators:
!!!number
... [very long list]
+number >> +number

and so on. This list never ends, but you can presume that if there is
an answer it will crop up reasonably soon (hint: give up once you've
got to +number >> +number using the ordering above).

You don't say what type "number" has. If it has to work for a lot of
different types, you can cut this list down a lot. Working this way
may look tedious by you will learn a lot about how C's operators are
defined to work. Once you've done a few you'll be able to check them
very quickly.

Get extra credit by saying how many expressions there are in the list
for "n operators".
 
N

Noob

run this

#include <stdio.h>

int main (void)
{
printf ("%d %d\n", 1 == 0, 0 == 0);
return 0;
}

does that give a hint?

Nick,

Why are you replying to me?

NB: the "==" operator is not accepted in the OP's pointless game.
 
C

Chris M. Thomasson

Lakshmi Sreekanth said:
Hi,

My problem: I need to return 0 if number = 0 other wise return value
should be 1.

Condition: Only by using below operators, We should not use any loops
or if or switch or any thing

Legal ops: ! ~ & ^ | + << >>

Waiting for clue.

logical negation
 
M

Mug

Hint: the "!" operator maps 0 to 1 and everything else to 0.

my friend found this in 5 minutes in our xml course ;)

#include <stdio.h>
int main(int argc, const char *argv[])
{
int n=0;
printf("answer %d\n",!(!n));
return 0;
}

not need the explication i think
 
K

Keith Thompson

Mug said:
Hint: the "!" operator maps 0 to 1 and everything else to 0.

my friend found this in 5 minutes in our xml course ;)

#include <stdio.h>
int main(int argc, const char *argv[])
{
int n=0;
printf("answer %d\n",!(!n));
return 0;
}

not need the explication i think

Great. Now Lakshmi Sreekanth has the solution she(?) was looking
for, and is relieved of the terrible burden of either thinking
or learning.

I've got this project at work. Can you do that for me too?
Don't worry, I'll take all the credit.
 
K

Kenny McCormack

Keith Thompson said:
Great. Now Lakshmi Sreekanth has the solution she(?) was looking
for, and is relieved of the terrible burden of either thinking
or learning.

I've got this project at work. Can you do that for me too?
Don't worry, I'll take all the credit.

Actually, if you posted all the particulars here (or, if it is large,
which I doubt, given your general displayed skill level, post it on a
website for us all), somebody probably will do it all for you. Of
course, you will have to live with their solution, which may not be to
your liking. That's the price of getting things for free.

Just consider Linux...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top