Thread: Got a question about odd numbers

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

    Got a question about odd numbers

    Code:
    while (counter1 !=0 ,&(counter1&1===>counter1))
    am I doing this wrong? I am trying to compare the least significant bit to determine if counter1 is odd.
    Thanks

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    What do you think ",&" and "===>" are supposed to mean?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Um, is that supposed to be a valid snippet of code in C?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I'm not sure, though I was told it was a conditional test to see if a number is odd
    ,& means AND

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This could possibly be used to test i counter1 is even or odd: counter1&1

    But looking at what you posted... how did you come up with that code? Who told you that "it was a conditional test to see if a number is odd"? Did you randomly add in other characters yourself? What material are you using to learn C?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    it's something someone told me a while back as a simple test to determine if counter1 is odd instead of using a modulo calculation.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Once-ler View Post
    I'm not sure, though I was told it was a conditional test to see if a number is odd
    ,& means AND
    If you mean AND, that's "&&", not ",&". Someone may very well have told you that "counter1&1" is a test to see if a number is odd, because it is. The rest of it ....

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Well yes, there's that part that I highlighted. But now, what about the ===> operator? Where have you seen it?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    150
    I forgot that AND was && as to the operator I have it saved in my notes I really don't recall where I got it.
    Thank you for helping me.
    Last edited by Once-ler; 01-02-2014 at 10:05 AM.

  10. #10
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Throwing operators/functions that you think are related at a line of code and hoping it works isn't programming. You code can't even compile, because it makes no sense, so it doesn't pass even the first stage of actually DOING anything.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  11. #11
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The definition of an odd integer is "not exactly divisible by 2". Common sense would suggest a test of whether a value is divisible by 2 is sufficient.

    And that doing it with a bitwise operation is either unnecessary obfuscation or premature optimisation (or both, when someone writes code they themselves don't understand, as exhibited here).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  12. #12
    Registered User
    Join Date
    Nov 2013
    Location
    Silicon Valley, CA
    Posts
    7
    ,&??? you should use correct syntax.

  13. #13
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    Quote Originally Posted by grumpy View Post
    that doing it with a bitwise operation is either unnecessary obfuscation or premature optimisation.
    Are you serious?

  14. #14
    Registered User
    Join Date
    Apr 2013
    Posts
    1,658
    Quote Originally Posted by grumpy View Post
    The definition of an odd integer is "not exactly divisible by 2".
    So for a twos complement binary integer where the bits can be considered to be coefficients of a polynomial of powers of 2, then any number with the least significant bit clear must be exactly divisible by 2 since the non-zero coefficients are all multiplied by powers of 2 (so 2 is a common factor for all non-zero terms of such a binary polynomial), and any number with the least significant bit set must be odd.

    On any computer where binary numbers are two's complement, then this if statement tests to see if counter is odd:

    if(counter&1)

    this statement could also be used:

    if(0 != (counter&1)) ...

    Using modulo should work on any computer, even if it's numbers are not two's complement (which is grumpy's point, and the compiler will probably optimize this to use bit-wise operation if possible):

    if(0 != (counter%2))
    Last edited by rcgldr; 01-08-2014 at 06:48 AM.

  15. #15
    misoturbutc Hodor's Avatar
    Join Date
    Nov 2013
    Posts
    1,787
    I've written a non-optimized version

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <errno.h>
    #include <string.h>
    #include <math.h>
    
    enum {
        PARITY_ODD,
        PARITY_EVEN
    };
    
    
    int *primefactors(unsigned long long n);
    int parityeven(long long n);
    void fatalerr(int err);
    static int primefactors_r(unsigned n, int **mem, size_t *sz);
    
    
    int main(void)
    {
        int n;
        
        const char *paritystr[2] = {"odd", "even"};
        
        for (n = -10; n <= 11; n++)
            printf("%d is %s\n\n", n, parityeven(n)[paritystr]);
        
        return 0;
    }
    
    int parityeven(long long n)
    {
        int *pfacts;
        int i, r = PARITY_ODD;
        
        if (n < 0)
            n = -n;
        
        if (n == 0 || n == 2) 
            return PARITY_EVEN;
        if (n == 1 || n == 3)
            return PARITY_ODD;
        
        pfacts = primefactors(n);
        
        if (!pfacts)
            fatalerr(errno);
        
        if (pfacts[0] == 0)
            printf("Is prime");
        else
            printf("Prime Factors:");
    
        for (i = 0; pfacts[i] != 0; i++) {
            printf(" %d", pfacts[i]);
            if (pfacts[i] == 2) {
                r = PARITY_EVEN;
            }
        }       
        printf(" => ");
        return r;
    }
    
    void fatalerr(int err)
    {
        fprintf(stdout, "errr... %s\n", strerror(err));
        exit(1);
    }
    
    int *primefactors(unsigned long long n)
    {
        int *mem;
        size_t sz = 1;
        
        if ((mem = malloc(sizeof *mem * sz)) == NULL)
            return NULL;
        
        if (n < 4) {
            mem[0] = 0;
            return mem;
        }
        
        primefactors_r(n, &mem, &sz);
        mem[sz - 1] = 0;
        
        return mem;
    }
    
    static int primefactors_r(unsigned n, int **mem, size_t *sz)
    {
        int i, maxn;
        int *newmem; 
        
        if (n < 2)
            return 0;
        
        maxn = n / 2;
        for (i = 2; i <= maxn; i++) {
            if (n % i == 0) {
                if (primefactors_r(i, mem, sz)) {
                    (*mem)[*sz - 1] = i;
                    newmem = realloc(*mem, ++(*sz) * sizeof(**mem));
                    if (!newmem) {
                        int err = errno;
                        free(mem);
                        fatalerr(err);
                    }
                    *mem = newmem;
                }
                if (primefactors_r(n / i, mem, sz)) {
                    (*mem)[*sz - 1] = n / i;
                    newmem = realloc(*mem, ++(*sz) * sizeof(**mem));
                    if (!newmem) {
                        int err = errno;
                        free(mem);
                        fatalerr(err);
                    }
                }
                break;
            }
        }
        /* return 0 (false) if no 'lower' primefactors found;
         * i.e. the above loop didn't terminate early */
        return i > maxn;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question on hex numbers.
    By ahgan84 in forum C Programming
    Replies: 6
    Last Post: 06-24-2011, 05:34 PM
  2. question about enter only numbers
    By fugazi in forum C Programming
    Replies: 4
    Last Post: 10-22-2010, 02:42 PM
  3. Question about numbers
    By h3ro in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2007, 09:42 AM
  4. question (floating numbers)
    By Mak in forum C Programming
    Replies: 9
    Last Post: 10-14-2003, 09:05 PM
  5. Newbie question on numbers.
    By thes in forum C++ Programming
    Replies: 14
    Last Post: 06-17-2002, 07:18 AM