Thread: Simple Challenge

  1. #1
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68

    Simple Challenge

    Complete the function IsEven() with just 1 line of code and without using Mod (%), +,*,-, or any built in functions.

    NB. A line of code ends with a ';'

    Good luck =D

    Code:
    #include <iostream>
    using namespace std;
    
    bool isEven(int f)
    {
        //code here
    }
    
    int main (int argc, char * argv[])
    {
        int f;
        cout << "Enter # ";
        cin >> f;
        cout << f << ( isEven(f)  ?  " is even" : " is odd" ) << endl;
        return 0;
    }
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Even if you know the trick, it isn't guaranteed to work like mod... Mod works no matter what your system does to represent negative numbers.

  3. #3
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    Quote Originally Posted by whiteflags View Post
    Even if you know the trick, it isn't guaranteed to work like mod... Mod works no matter what your system does to represent negative numbers.
    that's true. at least you know the 'trick'.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    I can think of 3 ways to do this:

    binary & operator
    left/right shifting
    division/multiplication

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Using bit shifting amounts to multiplication or division, but that should indeed work.

  6. #6
    printf("Hello Cboard\n"); codeprada's Avatar
    Join Date
    Jan 2011
    Location
    In a little room at the back of your brain
    Posts
    68
    oh whiteflags it wasn't intended to work like mod. every even number's last bit is 0 and odd's are 1. so by ANDing the last bit you can figure out which of the two it is. it doesn't find the remainder if that's what u thought.
    We shouldn't be quick to criticize unless we can do better.
    Code:
    public function __clone() { die ( "I'm one of a kind" ); }

  7. #7
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    That's clearly not what he thought.

    He was saying that the "trick" may not work if your system doesn't use "Two's Complement" representation for negative numbers whereas using `mod' will.

    Soma

  8. #8
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Code:
    bool IsEven(int f)
    {
        return !(((f < 0) ? -f : f) & 1);
    }
    That trick solves the negative-number-representation problem.



    EDIT: Unless the representation is 1s Complement and f == -0 ( Yes, in 1s Complement 0 and -0 are different )
    EDIT^2: Anyway, what CPU uses 1s Compement nowadays?
    Last edited by GReaper; 02-12-2011 at 01:28 PM.
    Devoted my life to programming...

  9. #9
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    couldnt u just make another function to do it with several lines and put it in as
    Code:
    bool iseven(int f)
    {
          return eventest(f);
    }
    where eventest returns if it is even or not?

  10. #10
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Quote Originally Posted by bobknows View Post
    couldnt u just make another function to do it with several lines and put it in as
    Code:
    bool iseven(int f)
    {
          return eventest(f);
    }
    where eventest returns if it is even or not?
    that is just bending the rule to the breaking point.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  11. #11
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    yep :P also another way, use all the lines you want, just dont hit enter

    Code:
    /* some code */; /* some more code*/; /*even more code*/;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 05:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 05:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

Tags for this Thread