News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

odd or even?

Started by homerun-4, September 01, 2012, 10:54:16 AM

Previous topic - Next topic

homerun-4

Hi all, Im new to pelles c and i have to translate my pseudocode to c for a uni assignment. Just wondering how you translate the mod function (pseudocode) into something in c to determine whether a number is odd or even?

Stefan Pendl

Use the modulo operator as defined by the ANSI C standard.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

#2
There's another trick you can use that's easier than the mod function (%)...

Integers are stored as binary values thus, every other value has the low order bit set to 1...
So you can use ... odd = x & 1 ... to test for odd and even.

For a quick reference to C's special characters...

Help -> Contents -> C Language Reference -> Expressions and Assignments -> Operator Precidence

You might want to mark the page in your help favorites.