473,396 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Testing a number for evenness

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
Nov 15 '05 #1
9 1389
On Tue, 13 Sep 2005 12:21:41 +1000, "David White" <no@email.provided>
wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #2
Thanks for your informative and detailed reply.

DW
Nov 15 '05 #3
Jack Klein <ja*******@spamcop.net> writes:
[...]
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.

--
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.
Nov 15 '05 #4
Jack Klein <ja*******@spamcop.net> wrote:
In general, I never worry about non 2's complement implementations.


ITYM 1's complement implementations.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #5
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Jack Klein <ja*******@spamcop.net> wrote:
In general, I never worry about non 2's complement implementations.


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.)

--
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.
Nov 15 '05 #6
Jack Klein <ja*******@spamcop.net> wrote:
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
Nov 15 '05 #7
In article <dg**********@chessie.cirr.com>,
Christopher Benson-Manica <at***@nospam.cyberspace.org> wrote:


Jack Klein <ja*******@spamcop.net> wrote:
In general, I never worry about non 2's complement implementations.


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!
Nov 15 '05 #8
Keith Thompson <ks***@mib.org> wrote:
Christopher Benson-Manica <at***@nospam.cyberspace.org> writes:
Jack Klein <ja*******@spamcop.net> wrote:
In general, I never worry about non 2's complement implementations.
ITYM 1's complement implementations.

No, I think he means non 2's complement implementations.


I think I missed the word "non" in Jack's post and need new glasses.
*sigh*

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Nov 15 '05 #9
On Tue, 13 Sep 2005 10:36:44 GMT, rl*@hoekstra-uitgeverij.nl (Richard
Bos) wrote in comp.lang.c:
Jack Klein <ja*******@spamcop.net> wrote:
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


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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 15 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Rahul | last post by:
HI. Python , with its support of arbit precision integers, can be great for number theory. So i tried writing a program for testing whether a number is prime or not. But then found my function...
0
by: Billy Patton | last post by:
I have worked out a testing procedure that works well for me. Here it is for your taking. It has evolved over 15 years of c and perl coding and testing. My perl version looks VERY similar. It...
0
by: Chuck | last post by:
Sorry for the advertisement but I think this is something that people could actually use. I am creating a new product called Simple Load Testing and I thought I would see if anyone would be...
0
by: Brian Russell | last post by:
We have three servers (beyond my development box) in our organization. The first is a testing server that has IIS and SQL Server on it. The second is another testing server that also has IIS and...
72
by: Jacob | last post by:
I have compiled a set og unit testing recommendations based on my own experience on the concept. Feedback and suggestions for improvements are appreciated: ...
33
by: genc_ymeri | last post by:
Hi over there, Propably this subject is discussed over and over several times. I did google it too but I was a little bit surprised what I read on internet when it comes 'when to use what'. Most...
6
by: Dick | last post by:
I’d appreciate some advice regarding unit tests. My questions are general in nature but (as usual) best conveyed via a (simplified) example: I have a table that contains two columns – the...
24
by: David | last post by:
Hi list. What strategies do you use to ensure correctness of new code? Specifically, if you've just written 100 new lines of Python code, then: 1) How do you test the new code? 2) How do...
11
by: VK | last post by:
In the continuation of the discussion at "Making Site Opaque -- This Strategy Feasible?" and my comment at http://groups.google.com/group/comp.lang.javascript/msg/b515a4408680e8e2 I have...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.