Testing a number for evenness

D

David White

Since I've only programmed in C++ rather than C for a long time and I'm not
familiar with the C standard, I'd like to know if the function below is
guaranteed to work (including for negative numbers). I suspect that it
isn't, but I'd like to make sure:

int isEven(int n)
{
if((n & 1) == 1)
return 0;
else
return 1;
}

DW
 
J

Jack Klein

Since I've only programmed in C++ rather than C for a long time and I'm not
familiar with the C standard, I'd like to know if the function below is
guaranteed to work (including for negative numbers). I suspect that it
isn't, but I'd like to make sure:

int isEven(int n)
{
if((n & 1) == 1)
return 0;
else
return 1;
}

DW

No, it is most emphatically not guaranteed to work, not in C and not
in C++. C++ adopted C's three allowed representations for signed
integer types (they call it "inherited", we call it "misappropriated",
go figure).

This will work in any C or C++ implementation for unsigned integer
types. This will work in any C or C++ implementation for any signed
integer type with a positive value. But when you get to signed
integer types with a negative value...

It works for 2's complement. It works for signed magnitude. It is
wrong every single time for 1's complement, with the possible
exception of -0, if the implementation allows -0.

In general, I never worry about non 2's complement implementations.
Really, try and find one. But I wouldn't code it this way, anyway. It
has been at least 10 years since I have seen a compiler brain-dead
enough to not replace a division or modulo by a power of 2.

I would write it:

int isEven(int n)
{
return !(n % 2);
}

The fact that this would also work correctly on 1's complement
implementations, if I ever trip over one, is an extremely small
additional bonus.
 
K

Keith Thompson

Jack Klein said:
I would write it:

int isEven(int n)
{
return !(n % 2);
}

I find this a bit clearer:

int isEven(int n)
{
return n % 2 == 0;
}

I know it's semantically equivalent, but I prefer to use "!" for
things that are logically Boolean, and "== 0" (or "== NULL" or "== '\0')
for things that aren't.
 
K

Keith Thompson

Christopher Benson-Manica said:
ITYM 1's complement implementations.

No, I think he means non 2's complement implementations. The fact
that signed magnitude implementation don't have this particular
problem doesn't imply that he worries about them. (Just showing off
my mind-reading skills.)
 
R

Richard Bos

Jack Klein said:
No, it is most emphatically not guaranteed to work, not in C and not
in C++. C++ adopted C's three allowed representations for signed
integer types (they call it "inherited", we call it "misappropriated",
go figure).

Well, give Stroustrup credit where it's due: better well stolen than
badly invented.

Richard
 
A

Anonymous 7843

ITYM 1's complement implementations.

Come and knock on our door
We've been waiting for you
Where the kisses are hers and hers and his
Three's complement, too!
Come and dance on our floor
Take a step that is new
We've a loveable space that needs your face
Three's complement, too!
You'll see that life is a ball again and
laughter is callin' for you
Down at our rendezvous,
Three's complement, too!
 
J

Jack Klein

Well, give Stroustrup credit where it's due: better well stolen than
badly invented.

Richard

Believe me, I give Dr. Stroustrup a lot of credit. Or he can pay
cash.
 

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
474,169
Messages
2,570,919
Members
47,459
Latest member
Vida00R129

Latest Threads

Top