Even/Odd Number without % or /

This is a discussion on Even/Odd Number without % or / within the C++ Programming forums, part of the General Programming Boards category; Hi all, I'm new over here n want some help. What will be the solution, if we want to find ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    8

    Even/Odd Number without % or /

    Hi all,

    I'm new over here n want some help. What will be the solution, if we want to find out even/odd number without using divsion or modulus operator?

    Best Regards.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Use subtraction

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Consider the binary layout of an integer and look up the bitwise & (and) operator.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    You can read the number into a string stream, and then using a switch() statement if myStr[myStr.length() - 1] is equal to '1', '3', '5', '7', or '9', then the number is odd. If the number's not odd, then it's even.
    Last edited by 7stud; 03-13-2006 at 11:00 PM.

  5. #5
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Sure, but don't you think that uses the % operator?

    Use two mutually recursive functions!
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    Sure, but don't you think that uses the % operator?
    I know that it does not.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,307
    Quote Originally Posted by anonytmouse
    Consider the binary layout of an integer and look up the bitwise & (and) operator.
    Yes, do it the way everyone else does it:
    Code:
    if (x & 1)
    {
        cout << "OMFG x is ODD!!!" << endl;
    }
    else
    {
        cout << "OMFG x is EVEN!!!" << endl;
    }

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,343
    Not quite as efficient as x&1, but guaranteed to work regardless of how an int is represented;
    Code:
    int IsOdd(int x)
    {
          while (x > 0)  x -= 2;
          while (x < 0) x += 2;
          return x;
    }
    
    int IsEven(int x)
    {
         return !IsOdd(x);
    }

  9. #9
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by grumpy
    Not quite as efficient as x&1, but guaranteed to work regardless of how an int is represented;
    You say that as if x&1 won't work with all integers, but it surely will. &1 test for the 1st bit being 1, which for all odd integers this will be the case regardless of sign or type of integer.

    And since you brought up efficiency, if yours is passed a very large integer, how slow is that going to be? I would guess to slow for most practical purposes.
    Last edited by Darryl; 03-14-2006 at 07:03 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    And since you brought up efficiency, if yours is passed a very large integer, how slow is that going to be? I would guess to slow for most practical purposes.
    Of course, in practice if one suspects that x&1 might not work unless some circumstance, one would just choose x%2... but oranges isnt asking for something that will necessarily be used in practice.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Quote Originally Posted by Darryl
    You say that as if x&1 won't work with all integers, but it surely will. &1 test for the 1st bit being 1, which for all odd integers this will be the case regardless of sign or type of integer.
    For unsigned integers, x&1 will be the same as x%2. This is also true for positive values of signed integers, whether ones' complement, twos' complement, or sign-and-magnitude is the underlying representation. But negative ones' complement values will produce different results. I think that may have been what grumpy may have meant.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  12. #12
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Quote Originally Posted by laserlight
    Of course, in practice if one suspects that x&1 might not work under some circumstance, one would just choose x%2... but oranges isn't asking for something that will necessarily be used in practice.
    Ok even in theory, I think it's a bad solution :

    Code:
    bool isOdd(int n) {return n&1;}
    bool isOdd(int n) {return n << (8*sizeof(int)-1) ;}
    bool isOdd(int n) { bitset<sizeof(int)> b(n); return b.test(0);}
    and for one's complement systems
    bool isOdd(int n) { return abs(n)&1;}
    Last edited by Darryl; 03-14-2006 at 08:21 AM.

  13. #13
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Rethink this one too for ones' complement, and for CHAR_BIT != 8, and for padded integers (or is that one just a C possibility?).
    Code:
    bool isOdd(int n) {return n << (8*sizeof(int)-1) ;}
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  14. #14
    Registered User
    Join Date
    Jun 2005
    Posts
    6,343
    Quote Originally Posted by Dave_Sinkula
    For unsigned integers, x&1 will be the same as x%2. This is also true for positive values of signed integers, whether ones' complement, twos' complement, or sign-and-magnitude is the underlying representation. But negative ones' complement values will produce different results. I think that may have been what grumpy may have meant.
    You've earned the cigar. If our integer type uses ones-complement notation, x&1 will not detect that negative values of x are odd.

  15. #15
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Quote Originally Posted by 7stud
    I know that it does not.
    You have knowledge of the inner workings of every STL implementation? Okay...

    At least in the STL implementation that gcc uses, the modulo operator is in fact used.
    http://gcc.gnu.org/onlinedocs/libstd...ce.html#l00883
    There are 10 types of people in this world, those who cringed when reading the beginning of this sentence and those who salivated to how superior they are for understanding something as simple as binary.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  2. Even/Odd number.
    By eXeCuTeR in forum C Programming
    Replies: 5
    Last Post: 11-22-2007, 11:31 AM
  3. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  4. parsing a number
    By juancardenas in forum C Programming
    Replies: 1
    Last Post: 02-19-2003, 12:10 PM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 04:00 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21