The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
Even and Odd Numbers
Discuss Even and Odd Numbers in the C Programming forum on Dev Shed. Even and Odd Numbers C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 22nd, 2004, 03:51 PM
|
|
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
|

November 22nd, 2004, 04:03 PM
|
 |
Contributing User
|
|
|
|
Using the % operator you can determine whether a value is evenly divisible by two, and therefore even or odd.
Code:
if ( value % 2 )
{
/* odd */
}
|

November 23rd, 2004, 07:28 AM
|
|
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 */
}
|

November 23rd, 2004, 07:42 AM
|
 |
Lord of Dorkness
|
|
Join Date: Jan 2004
Location: Central New York. Texan via Arizona, out of his element!
|
|
|
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.
|

November 23rd, 2004, 12:40 PM
|
|
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?
|

November 23rd, 2004, 12:41 PM
|
|
Contributing User
|
|
Join Date: Jan 2004
Location: Constant Limbo
|
|
|
just read this thread...
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
|

November 23rd, 2004, 01:44 PM
|
|
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. 
|

November 23rd, 2004, 04:18 PM
|
 |
Contributing User
|
|
|
|
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:
http://groups.google.com/groups?sel...ws1.newsguy.com
|

November 24th, 2004, 08:27 AM
|
|
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.
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|