Logical operator not working in if condition

J

jameskuyper

Phil said:
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.

I'm not sure exactly what Kojak means by "the C specs", but if he
means the C standard, that would not be an appropriate response, and
will be insufficient to win the respect you mention. The standard is
written in standardese, a dialect of English that even experienced
programmers sometimes have trouble understanding. It's not well
written for the purpose of explaining things to less experienced
programmers. In general, a good response not only cites the relevant
text, but also explains what that text means in terms that are easier
to understand than the standard itself.

I confess that I often fail to come up with a good response; teaching
newbies about complicated things often requires lying about the
complications, something I've never been very good at.
 
C

CBFalconer

Nate said:
.... snip ...


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.

FINALLY somebody posted the real cause of the malfunction. After
about 30 fouled answers. Congratulations.
 
P

Peter Nilsson

Flash Gordon said:
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.

Surely, the simplest and _most_ portable solution for checking
even and odd is to actually test for even and odd-ness?!

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

Let the compiler take care of the optimisation.
 
N

Nate Eldredge

Nate Eldredge wrote:
[...]
if(value<<15) // for even or odd value every
time its taking +ve value only
[...]
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 */;

The alternate will always evaluate to zero, because it is equivalent to

if (value & (1 == 0)) /* always executed */;

Whoops, good catch. I see, in fact, that K&R explicitly warns about
this :)

What I would probably be inclined to write instead, anyway, would be

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

Flash Gordon

Peter said:
Surely, the simplest and _most_ portable solution for checking
even and odd is to actually test for even and odd-ness?!

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

Let the compiler take care of the optimisation.

True. I just could not be bothered with explaining about optimisers ;-)
 
P

Phil Carmody

Richard Heathfield said:
Phil Carmody said:


Not really. The goal is to be on the /right/ side of the fence, not
merely "the other side of the fence from Han". Otherwise, a troll
gets to choose which side of the fence you're on, which is silly.
Far better just to aspire to be right, helpful, and readable,
without reference to what the trolls are currently doing.

True. My faded memories of Han are those where every post had
some error in it, even if it was a small or pedantic one. You
and Keith et al., always seemed to be on the opposite side to
him by virtue of him being wrong, and you being right. I have
suffered a sampling artefact or performed a rounding error in
drawing my above aberrant conclusion.

Phil
 
R

Richard

Flash Gordon said:
True. I just could not be bothered with explaining about optimisers
;-)

You said they were off topic.

And have nothing to do with the issue.
 
T

Tim Rentsch

Richard Heathfield said:
Kojak said:


Please explain how this is supposed to work on systems where
unsigned shorts are wider than 16 bits.

Is it being graded on a curve? It gets the right answer
for more than half the inputs.

;)
 

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
474,434
Messages
2,571,685
Members
48,796
Latest member
Greg L.

Latest Threads

Top