How to select odd and even for the input

Discussion in 'Embedded Systems and Microcontrollers' started by cpleng7, Dec 22, 2010.

  1. cpleng7

    Thread Starter Active Member

    Dec 18, 2008
    120
    0
    May I know how to program the detect the microcontroller input odd or even
     
  2. Markd77

    Senior Member

    Sep 7, 2009
    2,803
    594
    Could you expand on that please? I don't understand the question.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,014
    Do you mean high and lo?

    What kind of mcu?
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Test the least significant bit. If it is a 0, number is even, if it is a 1, number is odd.

    eg:
    210 = 102 (LSB=0, number is even)

    The only special case is zero, which is both odd and even.

    A more complicated test is to divide number by 2, and check the remainder (mod2). If there is a remainder, number is odd, if not, it is even. The test for zero needs to come before this though.
     
Loading...