Logical operator not working in if condition

V

venkatarao0

file.c

#include"stdio.h"

main()
{
unsigned short value;
scanf("%d",&value);
if(value<<15) // for even or odd value every
time its taking +ve value only
printf("given odd value");
else
printf("given even value");
}

output:

for given even or odd value its giving "given odd value" every time
why logical operator not working in if condition.

file.c

#include"stdio.h"

main()
{
unsigned short value,a;
scanf("%d",&value);
a = value<<15;
if( a ) // In this condition working fine
printf("given odd value");
else
printf("given even value");
}
 
K

Kojak

Le Tue, 17 Feb 2009 21:16:13 -0800 (PST),
(e-mail address removed) a écrit :
#include"stdio.h"

main()
{
unsigned short value;
scanf("%d",&value);
if(value<<15)
printf("given odd value");
else
printf("given even value");
}

for given even or odd value its giving "given odd value" every time
why logical operator not working in if condition.

Cast (value<<15) like this :
if ((unsigned short)(value<<15))
 
N

Nate Eldredge

Hi venkatarao,

A few comments inline, some of which are related to your problem and
others not.

file.c

#include"stdio.h"

This should read

#include said:

This should read

int main(void)

since we are not in the year 1985.
{
unsigned short value;
scanf("%d",&value);

The "%d" specifier expects to store an `int', but you have passed the
address of an `unsigned short'. The specifier to read an unsigned short
in decimal is "%hu". Also, it would be wise to check the return value
from scanf to make sure that it successfully read a number before
continuing and using that number.
if(value<<15) // for even or odd value every
time its taking +ve value only

This is the key to the problem.

The operands of << are promoted to `int' (or perhaps `unsigned int' or a
larger type) before shifting, and the result is of the same type. This
is most likely larger than 16 bits on your system, so shifting by 15
bits will not get rid of all the higher bits.

As someone else posted, you could do

if ((unsigned short)(value << 15))

If `unsigned short' is indeed 16 bits on your system, this will discard
all but the lowest 16, which should give you an expression which is 0 if
`value' was even, and 32768 if `value' is odd. However, `unsigned
short' is allowed to be larger than 16 bits (though not smaller), so
this won't work either on a system where that is the case.

A much better way to test whether a number is even or odd is to do

if (value % 2 == 0) /* even */; else /* odd */;

or alternatively

if (value & 1 == 0) /* even */; else /* odd */;

These will work no matter what integer type you use for `value'. They
might also be faster; on some systems, shifts are substantially slower
than the logical AND which these two examples will probably use.
printf("given odd value");
else
printf("given even value");

You should include a newline '\n' at the end of your output, otherwise
it will look funny on some systems and cause more severe problems on
others.

After changing the definition of `main' to `int main(void)', you will
need to return a value from the function. `return 0;' or else
output:

for given even or odd value its giving "given odd value" every time
why logical operator not working in if condition.

file.c

#include"stdio.h"

main()
{
unsigned short value,a;
scanf("%d",&value);
a = value<<15;
if( a ) // In this condition working fine
printf("given odd value");
else
printf("given even value");
}

Assigning `value << 15' to a variable of type `unsigned short' also has
the effect of truncating the high bits. But this also will not work if
`unsigned short' is more than 16 bits.
 
F

Flash Gordon

Kojak said:
Le Tue, 17 Feb 2009 21:16:13 -0800 (PST),
(e-mail address removed) a écrit :


Cast (value<<15) like this :
if ((unsigned short)(value<<15))

Then learn that there is a simpler and more portable solution
value & 1
This does not rely on the number of bits in a short.
 
K

Kojak

Le Wed, 18 Feb 2009 07:26:27 +0000,
Flash Gordon a écrit :
Then learn that there is a simpler and more portable solution
value & 1
This does not rely on the number of bits in a short.

Yes, I know, so what ?
 
K

Kojak

Le Wed, 18 Feb 2009 08:09:46 +0000,
Richard Heathfield a écrit :
Please explain how this is supposed to work on systems where
unsigned shorts are wider than 16 bits.

The OP asked why {a = value<<15; if( a )} works and not
{if(value<<15). I keep his code and simply add the way
to understand why. For the future, he will learn as it
will deepen the C. That's all!
 
K

Kenny McCormack

Le Wed, 18 Feb 2009 08:09:46 +0000,
Richard Heathfield a écrit :


The OP asked why {a = value<<15; if( a )} works and not
{if(value<<15). I keep his code and simply add the way
to understand why. For the future, he will learn as it
will deepen the C. That's all!

Welcome to CLC, Jacques.

We hope you enjoy your stay.
 
G

Guest

Le Wed, 18 Feb 2009 07:26:27 +0000,
Flash Gordon a écrit :

Yes, I know, so what ?

the code was
if(value<<15)
printf("given odd value");
else
printf("given even value");
}

so your suggestion to just cast the shifted value to unsigned short
*won't work* if short is more than 16 bits. Hence Flash Gordon's
solution
is better than yours.

It's a damn sight easier to understand as well...
Though I think (value % 2 == 1) is even clearer
 
K

Kojak

Le Wed, 18 Feb 2009 02:56:16 -0800 (PST),
(e-mail address removed) a écrit :
the code was...

You seem obsessed with the code and have forgotten to
include the question of the OP! Did you at least read
(and understood) the original question?

YES 'anding' is better then 'shifting' but it didn't
fall under the OP's question.

Then, don't waste your time to explain me what I already
know. I'm sure you have better to do.

Bye,
 
K

Kenny McCormack

Le Wed, 18 Feb 2009 10:35:21 +0000 (UTC),
Kenny McCormack a écrit :


Thank you, Kenny.

You are most welcome, but I hope you caught the irony in my post.

If not, email me privately...
 
K

Kojak

Le Wed, 18 Feb 2009 04:39:03 -0700 (MST),
Han from China a écrit :
Although I find it helpful to point out the 16-bit assumption (for
the OP, not for you, since I think it was obvious you knew about
the assumption), I'm sorry you had to get the typical comp.lang.c
smugness along with it. Their trolling/baiting is best ignored.
Rest assured that if one of their own had written exactly the
same thing you had, there wouldn't have been a whisper about it.
That's the current intellectual environment of this newsgroup.

Next time, I will give only the link to the C specs, like that,
there will be no more problem... :-D

Yours truly,
 
K

Kenny McCormack

Kojak said:
You seem obsessed with the code and have forgotten to
include the question of the OP!

Welcome to CLC. We hope you enjoy your stay.
Did you at least read
(and understood) the original question?

It is likely that he read it, and sort of understood it.
But what CLC regs do is immediately translate what they read into one of
their pre-defined cubby-holes. It is one of those "What you say to your
dog; what the dog hears" sort of things.

This "internal translation" happens all the time when CLC regs read
normal people's posts on CLC.
YES 'anding' is better then 'shifting' but it didn't
fall under the OP's question.

Exactly so.
Then, don't waste your time to explain me what I already
know. I'm sure you have better to do.

The truly sad truth is: No, they don't. They have no lives.
 
K

Kojak

Le Wed, 18 Feb 2009 12:54:01 +0000 (UTC),
Kenny McCormack a écrit :
Welcome to CLC. We hope you enjoy your stay.

I was not sure, but now I guess what you mean. I will try
to avoid turbulence... :)

It is likely that he read it, and sort of understood it.
But what CLC regs do is immediately translate what they read into one
of their pre-defined cubby-holes. It is one of those "What you say
to your dog; what the dog hears" sort of things.

Pavlov effect ? :-D

This "internal translation" happens all the time when CLC regs read
normal people's posts on CLC.

I've already seen this kind of behavior in others group
too... :-/

[...]
Then, don't waste your time to explain me what I already
know. I'm sure you have better to do.

The truly sad truth is: No, they don't. They have no lives.

Indeed, what a sad lives!


Sincerely,
 
R

Richard

the code was


so your suggestion to just cast the shifted value to unsigned short
*won't work* if short is more than 16 bits. Hence Flash Gordon's
solution
is better than yours.

It's a damn sight easier to understand as well...
Though I think (value % 2 == 1) is even clearer

Your inability to see outside the code example is simply astonishing.
 
B

Bartc

Kojak said:
Le Wed, 18 Feb 2009 08:09:46 +0000,
Richard Heathfield a écrit :


The OP asked why {a = value<<15; if( a )} works and not
{if(value<<15). I keep his code and simply add the way
to understand why. For the future, he will learn as it
will deepen the C. That's all!

Some experts on c.l.c like to give little lectures and worry about
portability of code to a myriad other systems.

There is nothing wrong with this but sometimes they forget an OP might just
has a specific query he wants answered quickly.
 
G

Guest

Le Wed, 18 Feb 2009 02:56:16 -0800 (PST),
(e-mail address removed) a écrit :

I'm probably going to regret this...
You seem obsessed with the code and have forgotten to
include the question of the OP!

you seem obsessed with your response and have also forgotten to
include the OP's question.

this is from the OP's post
#include"stdio.h"

main()
{
unsigned short value;
scanf("%d",&value);
if(value<<15)
printf("given odd value");
else
printf("given even value");
}

for given even or odd value its giving "given odd value" every time
why logical operator not working in if condition.

Did you at least read
(and understood) the original question?

you actually have a point here. I was so stuck on
"why the hell would anyone do a shift right 15 to
test for oddness?" that I failed to notice the slight
difference between the two pieces of code posted.

What I now note is that "cast it to short" doesn't actually
answer the OP's question either!


YES 'anding' is better then 'shifting' but it didn't
fall under the OP's question.

actually remaindering is *even* (odder?) better
 
K

Kojak

Le Wed, 18 Feb 2009 07:34:26 -0800 (PST),
(e-mail address removed) a écrit :

Sorry, but I have better things to do, then :

end of the dialogue!

Rest in your wonderfull world,

Yours truly,
 
J

jameskuyper

Kojak said:
Le Wed, 18 Feb 2009 02:56:16 -0800 (PST),
(e-mail address removed) a écrit :


You seem obsessed with the code and have forgotten to
include the question of the OP! Did you at least read
(and understood) the original question?
Yes.

YES 'anding' is better then 'shifting' but it didn't
fall under the OP's question.

It's neither necessary nor even appropriate to restrict our answers to
those which directly address the question being asked. Often, the
person who is asking the question is asking the wrong question, in
which case the most helpful thing to do is to explain why, and to
answer the question the OP should have asked. In addition, it's not
uncommon for code presented as part of a question to contain defects
other than the ones that the OP is actually aware of. It's helpful to
point out those other defects, even though such help is not directly
relevant.

Your answer provided a solution only to the question that was actually
asked. Your solution will work on most real world systems, but not
all, and you didn't even bother to explain why the fix was needed. You
also didn't explain the portability limitations of the answer you did
give, but I suppose that's only to be expected from someone who's
willing to give an unnecessarily unportable answer to such a question.

Nate pointed out the other defects, explained why the code wasn't
working, and suggested an alternative approach that was both easier to
write and to understand, and is completely portable.

I know which answer I'd prefer to get, were I still in need of an
answer to such questions.
 
P

Phil Carmody

Kojak said:
Le Wed, 18 Feb 2009 04:39:03 -0700 (MST),
Han from China a écrit :

If one of the respected regulars had written that, then the clock
would strike 13, and water would start running uphill.

It's entirely possible that there too would not have been a whisper
about it from the rest of the respected regulars, but that'would
indicate that they were more interested in the laws of physics being
broken than in the newsgroup post.
Next time, I will give only the link to the C specs, like that,
there will be no more problem... :-D

If it's the right link, then that's a perfectly adequate response.
(Though citing an appropriate sentence, or even pinpointing a few
pertinent words would be even better.) Enough of those, and you'd
become a respected regular too. Then Han would criticise you too.

However, being on the other side of the fence from Han is actually
something positive to aspire too, so don't worry about that.

Phil
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top