Shop OBEX P1 Docs P2 Docs Learn Events
How to compare Odd and Even number on binary and decimal? — Parallax Forums

How to compare Odd and Even number on binary and decimal?

rmanzermanze Posts: 2
edited 2011-01-06 09:21 in General Discussion
HEllo,

I'm new here, I want to know how can i compare between Odd and Even number with both binary and decimal?

let say the binary input comes from Pin-0 to pin-4 and how can i convert this input into decimal and then compare between Odd and Even? pls help me

Thanks

Comments

  • bill190bill190 Posts: 769
    edited 2011-01-06 05:47
    For things like serial communications, it is common to determine if the 1's / 0's in a number are odd or even so you can add a "parity bit". (Good lesson on binary bit manipulation.)

    This would be done in assembly language or C.

    Some languages might have an "ISODD" or "ISEVEN" function.

    To find examples, google "parity check C" or google "isodd".

    Following is a parity check function in C, but this is checking ALL the bits. You would just need to check the least significant bit...

    i.e.
    00000000 = even
    00000001 = odd

    // ParityCheck Function
    unsigned char ParityCheck(unsigned char EightBitChar)
    {

    unsigned int Tmp;

    // Shift left 4 bits to right nibble of byte (0's will fill left nibble), store to Tmp.
    Tmp = (EightBitChar >> 4);

    // XOR left and right nibbles of EightBitChar - Any 1's will cancel theselves out.
    EightBitChar = (EightBitChar ^ Tmp);

    // Shift left 2 bits of right nibble to far right of byte (0's will fill left 2 bits), store to Tmp.
    Tmp = (EightBitChar >> 2);

    // XOR right two bits of EightBitChar and Tmp - Any 1's will cancel theselves out.
    EightBitChar = (EightBitChar ^ Tmp);

    // Shift 2nd bit to far right of byte (0 will fill far left bit), store to Tmp.
    Tmp = (EightBitChar >> 1);

    // XOR right bit of EightBitChar and Tmp - Any 1's will cancel theselves out.
    EightBitChar = (EightBitChar ^ Tmp);

    // AND with mask to remove all 1's except far right bit (leave that bit a 1 or 0).
    EightBitChar = (EightBitChar & 0b00000001);

    // Return remaining bit which should be odd 1 or even 0.
    return EightBitChar;
    }
  • rmanzermanze Posts: 2
    edited 2011-01-06 06:00
    Hello Bill,

    Thanks for your reply i think this might help me, anyway your explaination will be my reference..

    thanks a lot.
  • ercoerco Posts: 20,260
    edited 2011-01-06 08:27
    Using integer math, you can test for odd versus even simply by dividing by two then multiplying by two. If the new number=old number, the original number was even. If newnumber<>oldnumber, it was odd.
  • Heater.Heater. Posts: 21,230
    edited 2011-01-06 08:38
    Use the AND operator "&".
    If you AND your variable with 1 the result will be zero if the number is even, it will be one if it is odd.
    if variable & 1
        'Comes here if ODD
    else
       'Comes here if EVEN
    

    As you numbers are coming from pins P0 to P4 just read them from INA. P0 will have to be the least significant bit for the test above to work.
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2011-01-06 08:42
    It's not clear from the OP whether he means even/odd numbers or even/odd parity. Anyway, both solutions have been presented. But I would be curious which he really meant.

    -Phil
  • Spiral_72Spiral_72 Posts: 791
    edited 2011-01-06 09:21
    Heater. wrote: »
    Use the AND operator "&".
    If you AND your variable with 1 the result will be zero if the number is even, it will be one if it is odd.
    if variable & 1
        'Comes here if ODD
    else
       'Comes here if EVEN
    

    As you numbers are coming from pins P0 to P4 just read them from INA. P0 will have to be the least significant bit for the test above to work.

    Genius! Why didn't I think of that?? I had this problem the other day and your solution is far more elegant than mine.
Sign In or Register to comment.