Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support |

#1
November 22nd, 2004, 03:51 PM
 kalkap
Registered User

Join Date: Nov 2004
Posts: 10
Time spent in forums: < 1 sec
Reputation Power: 0
Even and Odd Numbers

how do you differentiate even and odd numbers in ANSI C? I have to enter a bunch of numbers and then make the program count how many of those numbers were odd and how many were even.

thanks

#2
November 22nd, 2004, 04:03 PM
 Dave Sinkula
Contributing User

Join Date: Apr 2004
Posts: 1,676
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Using the % operator you can determine whether a value is evenly divisible by two, and therefore even or odd.
Code:
```if ( value % 2 )
{
/* odd */
}```

#3
November 23rd, 2004, 07:28 AM
 pflloyd
Contributing User

Join Date: Oct 2004
Posts: 150
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
instead of doing modulo arithmetic, which has to do division every time, just to a bit AND. This is a quicker way of doing the same check. If the least significant bit is 1 your number is odd.

Code:
```if ( value & 0x01 )
{
/* odd */
}
else
{
/* even */
}```

#4
November 23rd, 2004, 07:42 AM
 DaWei_M
Lord of Dorkness

Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
Posts: 8,515
Time spent in forums: 4 Weeks 18 h 59 m 31 sec
Warnings Level: 20
Number of bans: 3
Reputation Power: 3268
pffloyd, you apparently missed out on "EVEN AND ODD NUMBERS, THE SEQUEL." I would guess this is a dead horse....
__________________
Functionality rules and clarity matters; if you can work a little elegance in there, you're stylin'.
If you can't spell "u", "ur", and "ne1", why would I hire you? 300 baud modem? Forget I mentioned it.
DaWei on Pointers Politically Incorrect.

#5
November 23rd, 2004, 12:40 PM
 pflloyd
Contributing User

Join Date: Oct 2004
Posts: 150
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
aparrently I didn't. can ya fill me in?

#6
November 23rd, 2004, 12:41 PM
 L7Sqr
Contributing User

Join Date: Jan 2004
Location: Constant Limbo
Posts: 989
Time spent in forums: 2 Weeks 2 Days 22 h 45 m 6 sec
Reputation Power: 362
http://forums.devshed.com/t204206/s.html
__________________
True happiness is not getting what you want, it's wanting what you've already got.

My Blog

#7
November 23rd, 2004, 01:44 PM
 pflloyd
Contributing User

Join Date: Oct 2004
Posts: 150
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
ok I read that, but since some one else already answered his question, though I would improve upon it.

#8
November 23rd, 2004, 04:18 PM
 Dave Sinkula
Contributing User

Join Date: Apr 2004
Posts: 1,676
Time spent in forums: 1 Month 3 Days 8 h 23 m 46 sec
Reputation Power: 132
Quote:
 Originally Posted by pflloyd ok I read that, but since some one else already answered his question, though I would improve upon it.
Please allow me to (point to a posting that may further) expand on yours:

#9
November 24th, 2004, 08:27 AM
 pflloyd
Contributing User

Join Date: Oct 2004
Posts: 150
Time spent in forums: 2 Days 2 h 7 m 54 sec
Reputation Power: 10
yes any good compiler should make this substitution. I've been using the ImageCraft C Compiler for an embedded system that runs on a Motorola HC12 controller, and the compiler does not make this optimization.

 Viewing: Dev Shed Forums > Programming Languages > C Programming > Even and Odd Numbers