Pelles C forum

C language => Beginner questions => Topic started by: homerun-4 on September 01, 2012, 10:54:16 AM

Title: odd or even?
Post by: homerun-4 on September 01, 2012, 10:54:16 AM
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?
Title: Re: odd or even?
Post by: Stefan Pendl on September 01, 2012, 11:35:06 AM
Use the modulo operator as defined by the ANSI C standard.
Title: Re: odd or even?
Post by: CommonTater on September 01, 2012, 04:25:45 PM
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.