Thread: How do I find if a number is exactly divisable by 2?

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    150

    How do I find if a number is exactly divisable by 2?

    I am a little frustrated by this I am sure that there is an easy way to do this?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Code:
    double number = whatever / 2.0;
    double wholepart;
    double fracpart;
    wholepart = modf(number, &fracpart);
    if (fracpart == 0.0)
       divisible
    else
       not
    
    int number = whatever % 2;
    if (number == 0)
       divisible
    else
       not
    Last edited by whiteflags; 01-02-2012 at 04:44 AM. Reason: cide correction

  3. #3
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    int number;
    
    number = 42;
    
    if ((number & 1) == 0)
      puts("Divisible by two");
    All integers are stored as binary values... bit 0, is always a 1 for odd numbers, only even numbers are divisible by 2.
    So... if bit0 = 0 the number is divisible by 2.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Nice. No effort at all on the OP's part, and he has two options already. Guess I may as well round this out:
    Code:
    r = num / 2;
    if( r * 2 == num )
        Yay!
    else
        Nay!

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok... how about...
    Code:
    if ( ! (num - ((num >> 1) << 1)))
      puts("Yup");
    else
      puts("Naaaa");
    Last edited by CommonTater; 01-02-2012 at 05:42 AM.

  6. #6
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Code:
    #include <limits.h>
    #include <math.h>
    
    ...
    
    int power;
    
    power = log( (double) UINT_MAX + 1 ) / log( 2.0f ) - 1;
    
    if( ( num << (power - 1) ) >> (power - 1)  )
            //Odd
    else
            //Even
    I don't work with floating point a lot, but I think this would work?
    Last edited by DeadPlanet; 01-02-2012 at 06:01 AM. Reason: Doublifying my casts

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    or
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    int main(int argc,char *argv[])
    {
            double x;
            int a;
            int b;
    
            x = 5.0;
            x = fmod(x,2);
            printf("%d\n",x == 0.0);
            x = 4.0;
            x = fmod(x,2);
            printf("%d\n",x == 0.0);
            x = 4.0000001;
            x = fmod(x,2);
            printf("%d\n",x == 0.0);
    
            a = 5;
            b = a % 2;
            printf("%d\n",b==0);
            a = 4;
            b = a % 2;
            printf("%d\n",b==0);
    
            return 0;
    }

  8. #8
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Whoever writes the fanciest program gets a cookie.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I like cookies...

    Code:
    puts(num - ((num >> 1)<<1) ? "No" : "Yes");

  10. #10
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    CommonTater is in the lead.
    Quote Originally Posted by The Jargon File
    Microsoft Windows - A thirty-two bit extension and graphical shell to a sixteen-bit patch to an eight-bit operating system originally coded for a four-bit microprocessor which was written by a two-bit company that can't stand one bit of competition.

  11. #11
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Code:
    const char* answer[] = {"Yes", "No");
    puts(answer[num&1]);
    Oh wait wait...
    Code:
    puts("Yes\0No"+((num&1)<<2));
    That's better.

    Edit: Aaargh, got it this time.
    Last edited by iMalc; 01-02-2012 at 02:03 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by iMalc View Post
    Code:
    puts("Yes\0No"+((num&1)<<2));
    I'll give you the lead after that one...

    (Darn, I really wanted that cookie!)

  13. #13
    Registered User gardhr's Avatar
    Join Date
    Apr 2011
    Posts
    151
    Quote Originally Posted by CommonTater View Post
    All integers are stored as binary values...
    Except when they aren't, of course.

    Code:
    #include<stdio.h>
    int divisible_by_two(long int data)
    {
    	return !(data % 2L);
    }
    int main(int argc, char** argv)
    {
    	char* errc;
    	while(*(++argv))
    		printf("%s is%s divisible by two\n", *argv, "\0 not" + !(divisible_by_two(strtol(*argv, &errc, 0)) - !!*errc));
    	return 0;
    }
    I win.

  14. #14
    Registered User
    Join Date
    Sep 2011
    Posts
    52
    Is there a possibility to swap the integer, shift it left, and watch the carry flag? That's the most fancy way i can imagine...
    EDIT: Why don't use the parity flag..
    Last edited by Libpgeak; 01-03-2012 at 03:55 AM.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gardhr View Post
    Except when they aren't, of course.
    Riiiiiight. there's one of them in everyone's livingroom.

    I win.
    Not up to you... this was Babcock's idea...
    He gets to judge.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to find maximum number
    By mutombo in forum C Programming
    Replies: 3
    Last Post: 02-27-2009, 04:36 AM
  2. Find first 250 Prime Number
    By dummies in forum C Programming
    Replies: 4
    Last Post: 01-12-2007, 09:18 AM
  3. Find all factors of a number
    By Spono in forum C Programming
    Replies: 5
    Last Post: 10-23-2003, 01:23 AM
  4. Find bigest number!!! Help!!!!!!
    By Chook in forum C Programming
    Replies: 13
    Last Post: 05-26-2003, 11:53 PM
  5. how do u find 2nd largest number??
    By juancardenas in forum C Programming
    Replies: 8
    Last Post: 02-14-2003, 08:28 AM