NO

Author Topic: odd or even?  (Read 2130 times)

homerun-4

  • Guest
odd or even?
« 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?

Offline Stefan Pendl

  • Global Moderator
  • Member
  • *****
  • Posts: 582
    • Homepage
Re: odd or even?
« Reply #1 on: September 01, 2012, 11:35:06 AM »
Use the modulo operator as defined by the ANSI C standard.
---
Stefan

Proud member of the UltraDefrag Development Team

CommonTater

  • Guest
Re: odd or even?
« Reply #2 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.
 
« Last Edit: September 01, 2012, 05:19:18 PM by CommonTater »