Detect Even Number

This is a discussion on Detect Even Number within the alt.comp.lang.php forums, part of the PHP Programming Forums category; Is there a function in PHP to detect even or odd numbers? I need a quick and easy way to ...


Go Back   Usenet Forums > PHP Programming Forums > alt.comp.lang.php

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-31-2008
huwpioden@gmail.com
 
Posts: n/a
Default Detect Even Number

Is there a function in PHP to detect even or odd numbers? I need a
quick and easy way to detect even numbers in a script I'm writing.
Maybe phrased the search wrong but searching the manual doesn't come
up with anything useful.

TIA

Huw
Reply With Quote
  #2 (permalink)  
Old 03-31-2008
Olaf Schinkel
 
Posts: n/a
Default Re: Detect Even Number

huwpioden@gmail.com schrieb:
> Is there a function in PHP to detect even or odd numbers? I need a
> quick and easy way to detect even numbers in a script I'm writing.
> Maybe phrased the search wrong but searching the manual doesn't come
> up with anything useful.
>
> TIA
>
> Huw

Yes :-) In every langauge.
It is called AND (&)

$a = 5;
if ($a&1){// is odd}
else
{// is even}

Reply With Quote
  #3 (permalink)  
Old 04-01-2008
huwpioden@gmail.com
 
Posts: n/a
Default Re: Detect Even Number

On Mar 31, 2:27 pm, Olaf Schinkel <blabla...@bluxxxxxxxx.de> wrote:
>
> Yes :-) In every langauge.
> It is called AND (&)
>
> $a = 5;
> if ($a&1){// is odd}
> else
> {// is even}


Thanks Olaf. I can up with a different solution eventually - but sort
of based on the same idea. Thanks anyway.

Huw
Reply With Quote
  #4 (permalink)  
Old 04-02-2008
C.
 
Posts: n/a
Default Re: Detect Even Number

On 31 Mar, 14:27, Olaf Schinkel <blabla...@bluxxxxxxxx.de> wrote:
> huwpio...@gmail.com schrieb:> Is there a function in PHP to detect even or odd numbers? I need a
> > quick and easy way to detect even numbers in a script I'm writing.
> > Maybe phrased the search wrong but searching the manual doesn't come
> > up with anything useful.

>
> > TIA

>
> > Huw

>
> Yes :-) In every langauge.
> It is called AND (&)
>
> $a = 5;
> if ($a&1){// is odd}
> else
> {// is even}


You should get out more Olaf - that won't work in most languages.

The mod operator is a far more sensible solution (portable across
strongly typed languages and applicable to different problems):

if ($val % 2) {
print "$val is odd\n";
} else {
print "$val is even\n";
}

This can also handle more generic cases (a multiple of N)

C.
Reply With Quote
  #5 (permalink)  
Old 04-02-2008
Michael Fesser
 
Posts: n/a
Default Re: Detect Even Number

..oO(C.)

>On 31 Mar, 14:27, Olaf Schinkel <blabla...@bluxxxxxxxx.de> wrote:
>>
>> Yes :-) In every langauge.
>> It is called AND (&)
>>
>> $a = 5;
>> if ($a&1){// is odd}
>> else
>> {// is even}

>
>You should get out more Olaf - that won't work in most languages.


Binary operations work in almost every language. The code just checks if
the least significant bit in the number is set, which is the fastest way
to do what the OP wants.

>The mod operator is a far more sensible solution


But inefficient, even if it doesn't really matter in this case.

Micha
Reply With Quote
  #6 (permalink)  
Old 04-02-2008
Sylvain SF
 
Posts: n/a
Default Re: Detect Even Number

C. wrote on 02/04/2008 01:56:
>>
>> It is called AND (&)
>>

> You should get out more Olaf


hmm, have a nice day, too.

> that won't work in most languages. The mod operator is a far more
> sensible solution (portable across strongly typed languages and
> applicable to different problems):


you believe typed languages do not support binary operator ?
you believe php is a typed language ?

an odd number, for almost all CPUs, is a number whose lower bit
is set to '1', full point, what could be the remaining part of
a (very expensive) modulus operation is irrelevant.

> if ($val % 2)


which is not a portable across all typed languages expression!
not all of them evaluate a numerical value as a boolean value.

> This can also handle more generic cases (a multiple of N)


do you mean cases other than "odd or even" ?
can you list these cases please ?

Sylvain.


Reply With Quote
  #7 (permalink)  
Old 04-17-2008
Cujo
 
Posts: n/a
Default Re: Detect Even Number

Sylvain SF wrote:

> an odd number, for almost all CPUs, is a number whose lower bit
> is set to '1', full point, what could be the remaining part of
> a (very expensive) modulus operation is irrelevant.


I agree with "mod" being the best solution, at least if context isn't known.

You *ARE* right with efficiency, but in most cases i'd trade that for
code readability UNLESS execution time was CRITICAL. And if time WAS
critical, I probably wouldn't be using PHP altogether for the task.

i'd use

if ($a % 2 === TRUE)

>> This can also handle more generic cases (a multiple of N)


> do you mean cases other than "odd or even" ?


I think he does.

> can you list these cases please ?


Of course. Being "odd" and "even" synonyms for "not a multiple of two"
and "a multiple of two" respectively, generic cases might be testing for
$a being multiple of numbers other than two.

IE:

if ($a % $b === TRUE), where $b can be any integer.

regards, f.
Reply With Quote
  #8 (permalink)  
Old 04-17-2008
Sylvain SF
 
Posts: n/a
Default Re: Detect Even Number

Cujo wrote on 17/04/2008 23:25:
>
> generic cases might be testing for
> $a being multiple of numbers other than two.


certainly and these cases do not fall under the how to "detect an even
number" question. thanks for these obvious and OOT comments.

Sylvain.
Reply With Quote
  #9 (permalink)  
Old 04-17-2008
Michael Fesser
 
Posts: n/a
Default Re: Detect Even Number

..oO(Cujo)

>You *ARE* right with efficiency, but in most cases i'd trade that for
>code readability UNLESS execution time was CRITICAL. And if time WAS
>critical, I probably wouldn't be using PHP altogether for the task.
>
>i'd use
>
>if ($a % 2 === TRUE)


This will always be FALSE, because the return type of the modulo
operator is not a boolean. If you want such a complete expression,
compare with an integer:

if ($a % 2 === 0) {
...
}

Micha
Reply With Quote
  #10 (permalink)  
Old 04-17-2008
Cujo
 
Posts: n/a
Default Re: Detect Even Number

Sylvain SF wrote:

> certainly and these cases do not fall under the how to "detect an even
> number" question.


Of course they don't, if they did we wouldn't be talking about handling
**more generic** cases in the first place, would we ?

> thanks for these obvious and OOT comments.


You're welcome.

regards, f.
Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 10:10 PM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0