Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts

    odd and even numbers

    does php have any built in functions to determine if a number is odd or even? I came across the below when doing some google searches. But is that the simpliest way?

    PHP Code:
    function is_odd($number) {
       return 
    $number 1// 0 = even, 1 = odd

    I'm looking to create an if statement, and if the number is odd, do X, or if even, do Y.
    Last edited by ptmuldoon; 11-16-2007 at 06:04 PM.

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    I'm not aware of such a function, but you could try this:

    PHP Code:
    function is_odd($n) {
       
    $test $n/2;  // divide the number you want to test by 2
       
    if(strpos($test,'.') === false) {  // if there's no decimal found in the result then the number must have been even.
          
    return false// so return false
       
    } else {  // otherwise we can assume the number is odd
          
    return true;  // so we return true
       
    }

    This is untested so make sure it works first!

    Also, this assumes that a decimal point will be used after the whole numbers and before the part numbers, (like "2.5"). If your server is in a non-english language this may not be the case. For example a French PHP installation may return 5/2 = 2,5 in which case the function above would have to be modified.
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #3
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    Wow, that is a... weird solution.

    PHP Code:
    function isOdd($n) {
        return 
    $n 2;


  • #4
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    Ptmuldoons function is the best. Bitwise and is faster than modulo.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #5
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    You're telling me that a division and a string search are faster than just a modulus operator (which is essentially a division)?

    Not to mention that this is a common way of determining odd/even. I'm sure there's more inventive methods, but this is the easiest to read.

  • #6
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    I did say "ptmuldoon" not "johnnyb"...
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #7
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Bitwise is probably the fastest, but the compiler might simply optimize it to use bitwise given the situation:
    Code:
    function is_odd ($n) {
        return ($n & 1);
    }
    Why does it work? All it does is compare the bits in the ones place. If they are the same, the AND operation returns 1. Otherwise, it returns 0. Obviously if something is even, then the last bit would be 0 since binary is the base-2 system, which is even in itself. A quick table should help:
    0 = 000 = even
    1 = 001 = odd
    2 = 010 = even
    3 = 011 = odd
    4 = 100 = even
    5 = 101 = odd
    6 = 110 = even
    7 = 111 = odd

    See? Every time the number is odd, the very last bit (the bit in the ones place) is 1. I won't get any more specific than this since it would go into the AND truth table and stuff.

    An interesting thing to note, which may not be practiced much anymore: when you have $n % $m, where $m is a power of 2 (1, 2, 4, 8, 16, etc.), you can use $n & ($m - 1). Example:
    Code:
    $n = 40;
    echo $n % 16, '<br />';
    echo $n & 15; //16-1=15 obviously
    See? Same result, right? The only thing to note is that the '&' operator has lower precedence than ==, !=, etc., so if you have ($n & 1 != 1), for example, the result will be equal to $n & 0, which is 0. The right way to do it is to use (($n & 1) != 1). That way, $n & 1 gets evaluated first, instead of the 1 != 1. The difference is in the data type. If you need an explanation, just know that the comparison operators have a higher precedence than the bitwise operators - PHP: Operators - Operator Precedence
    Last edited by rpgfan3233; 11-16-2007 at 06:02 PM.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #8
    Regular Coder
    Join Date
    Apr 2003
    Location
    Montreal, QC
    Posts
    340
    Thanks
    3
    Thanked 2 Times in 2 Posts
    I never said that mine was fastest or best, just that it would probably work Actually, I forgot about modulo - I never really use it.

    The bitwise solution looks pretty nice to me.
    Search for Laughter or Just Search?
    GiggleSearch.org
    Blog: www.johnbeales.com
    All About Ballet: www.the-ballet.com

  • #9
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    I understand how the binary one works (I'd seen it before but never used so it hadn't 'stuck')

    Quote Originally Posted by marek_mar View Post
    I did say "ptmuldoon" not "johnnyb"...
    My visual compiler abstracted away the code in the original post, so I hadn't seen that he had posted that (or perhaps it was added afterwards since there was an edit). Hence I thought there was only mine and johnnyb's solution so I didn't think to look back up who you were talking about.

    My bad.

  • #10
    Regular Coder
    Join Date
    Sep 2007
    Location
    Raleigh, NC
    Posts
    273
    Thanks
    7
    Thanked 59 Times in 52 Posts
    Because of my old C days of bitflags I tend to use bitwise AND instead of MOD, but the question of what's fastest is kind of intriguing to me. Bitwise AND has the advantage of short-circuiting the evaluation if it runs into a 0, but I wonder if there's a bound where a really large number with all the bits on (like 1048575 which is 20 1s in binary) is actually less efficient than a small integer division (5 % 2)?

    Just musing. :P

  • #11
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by vtjustinb View Post
    Because of my old C days of bitflags I tend to use bitwise AND instead of MOD, but the question of what's fastest is kind of intriguing to me. Bitwise AND has the advantage of short-circuiting the evaluation if it runs into a 0, but I wonder if there's a bound where a really large number with all the bits on (like 1048575 which is 20 1s in binary) is actually less efficient than a small integer division (5 % 2)?

    Just musing. :P
    I'm not this old (thank goodness), but before there was a MOD instruction built into mainstream architectures, the bitwise AND existed as a short way of doing n MOD (2^m), but these days the speed difference is probably negligible, if there is a difference in the amount of time each takes.

    Honestly, I think there may be. If all that MOD does is use the DIV or IDIV instruction, which returns the quotient in the EAX register and the remainder (result of a MOD operation) in the EDX register, and moves the remainder from EDX to EAX and calls it a new instruction, I wouldn't be surprised. However, that would mean that AND is much faster. DIV takes about 14 clocks on an 80386 processor, while AND only takes 2 clocks. I sure we have really come far enough to have closed that gap to a more negligible speed.

    Back in those days, such a difference was probably a huge deal though. It is like creating a recursive function to calculate the 1024th Fibonacci number. It is much faster as an iterative function (loop) than as a recursive function (keep calling the function over and over and over until you get a stack overflow error like many people), not to mention how much less system intensive it is. I think that is why the bitwise AND trick became so widely adopted for a while, similar to how you can swap values with 3 bitwise XOR operations:
    a ^= b;
    b ^= a;
    a ^= b;

    Of course in C, you could just do a ^= b ^= a ^= b;

    Of course, creating a temporary variable is usually fast enough these days, not to mention that those operations might not get translated directly as one might expect. That's probably why inline Assembly code exists.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #12
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    similar to how you can swap values with 3 bitwise XOR operations:
    That's one of those tricks that shouldn't really be used. Simply because it creates unreadable code.

    I'm sure back then the difference in resources was worth it though.

  • #13
    Regular Coder
    Join Date
    Sep 2007
    Location
    Raleigh, NC
    Posts
    273
    Thanks
    7
    Thanked 59 Times in 52 Posts
    Quote Originally Posted by aedrin View Post
    That's one of those tricks that shouldn't really be used. Simply because it creates unreadable code.

    I'm sure back then the difference in resources was worth it though.
    Psht.. Code obfuscation is job security

  • #14
    Regular Coder
    Join Date
    Mar 2005
    Location
    D0u$h!t3 k4?
    Posts
    512
    Thanks
    2
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by vtjustinb View Post
    Psht.. Code obfuscation is job security
    LOL I've heard that one before, and I love hearing it.
    PHP Code:
    $hello file_get_contents('hello.txt'); echo $hello
    hello

  • #15
    Regular Coder
    Join Date
    Aug 2007
    Location
    maine,usa
    Posts
    151
    Thanks
    2
    Thanked 1 Time in 1 Post
    mod is the best way for that, I agree.
    Just a simple Developer and IT student
    My Blog
    My Portfolio
    My Playground of Technology


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •