Thread: Divisible by 2

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    38

    Divisible by 2

    Hello.

    Is there a better way to know if a number is even or odd than checking if it is divisible by two?

    ThankS

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Yes, there is another way, but I'm not sure if it's better. In fact, I suppose it may not be entirely portable.

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    What would be this other method?

  4. #4
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    if(number & 1)

    Its faster because it only checks if the 1s bit is set, but can have endian problems maybe. Not that I have experienced any with it as of yet.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by mike_g View Post
    if(number & 1)

    Its faster because it only checks if the 1s bit is set, but can have endian problems maybe. Not that I have experienced any with it as of yet.
    Is there a machine where integer variables and integer constants have different endianness? I can't see how that's possible.

  6. #6
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Tbh I don't know, and I guess you are probably right. I thought I had read that somewhere on this forum, but like I said I never experienced a problem with it myself.

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    38
    Thanks!

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    As was discussed not long time ago use portable way

    if(number % 2)

    and leave this optimization to compiler, when it is suitable - it will replace it by &1 byhimself
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    USA
    Posts
    516
    These days compilers optimize a lot of things, you are better off sticking to the normal way of checking if a number is divisible by 2.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    if(number & 1)

    Its faster because it only checks if the 1s bit is set, but can have endian problems maybe. Not that I have experienced any with it as of yet.
    On the list of unsafe things that's pretty low. I suppose it's a premature optimization, but I'm guilty of doing it all the time. I don't use it because it's "optimal" but because I understand what it means -- if the 1 bit is set, the number is odd. Just a basic fact, really.

  11. #11
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by brewbuck View Post
    if the 1 bit is set, the number is odd. Just a basic fact, really.
    While the number is unsigned integer type
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by brewbuck View Post
    On the list of unsafe things that's pretty low. I suppose it's a premature optimization, but I'm guilty of doing it all the time. I don't use it because it's "optimal" but because I understand what it means -- if the 1 bit is set, the number is odd. Just a basic fact, really.
    It works on all machines that use two's complement for negative numbers, but if your -1 value is 1111....1110 then the and with 1 will produce zero for -1, which is of course incorrect.

    If all numbers are positive [and that's quite commonly the case in these sort of situations], then you should be fine even for ones-complement machines.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by tabstop View Post
    Is there a machine where integer variables and integer constants have different endianness? I can't see how that's possible.
    Yes, it is. Custom architectures, for example. Nintendo 64 used low endian type I think, not sure. But the whole endianess story is because the processor really doesn't care. It doesn't matter if what way the bytes are stored because it works on bits only. So it can have one big complex mess of storing numbers, yet the processor can do its work just fine.

    So it might not seem feasible, but it is indeed so.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by tabstop View Post
    Is there a machine where integer variables and integer constants have different endianness? I can't see how that's possible.
    It wouldn't work very well, because MOST variables derive their current value from a constant in some way or another. So the constant and the variable must use the same bit & byte-order.

    Note also that byte-order doesn't change the bit-order of a number. Bit 0 is still the lowest bit, whether that's the byte stored to the left, right or up or down from the higher byte.

    It would make a difference if you do:
    Code:
    int x;
    char *p = (char *)&x;
    if (*p & 1) ...
    This assumes the first byte is the low byte. (Little endian)

    Even processors that allow endian-switching (e.g. 29K, Mips), the internal order of bits in a register remains the same, the only difference is when the data is stored to memory, it will either store the bytes as 31:24, 23:16, 15:8, 7:0 or 7:0, 15:8, 23:16, 31:24.

    --
    Mats

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vart View Post
    While the number is unsigned integer type
    Why would it be any different for signed numbers?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Prime number algorithm
    By Akkernight in forum C Programming
    Replies: 9
    Last Post: 04-19-2009, 01:50 PM
  2. Displaying Numbers divisible by user's desired number
    By DaniiChris in forum C Programming
    Replies: 9
    Last Post: 07-07-2008, 02:06 PM
  3. Simple C program displaying all numbers divisible by 6
    By DaniiChris in forum C Programming
    Replies: 25
    Last Post: 07-06-2008, 12:25 PM
  4. Help!!
    By ryang in forum C++ Programming
    Replies: 6
    Last Post: 11-21-2006, 12:45 AM
  5. C programming
    By annamayya in forum C Programming
    Replies: 20
    Last Post: 11-20-2004, 02:42 AM