Divisible by 2

This is a discussion on Divisible by 2 within the C Programming forums, part of the General Programming Boards category; Hello. Is there a better way to know if a number is even or odd than checking if it is ...

  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,211
    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,185
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  9. #9
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,246
    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
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    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
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,668
    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?

Page 1 of 2 12 LastLast
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-20-2006, 11:45 PM
  5. C programming
    By annamayya in forum C Programming
    Replies: 20
    Last Post: 11-20-2004, 01:42 AM

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