Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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

Report this thread to moderator Post Follow-up to this message
Old Post
huwpioden@gmail.com
03-31-08 01:27 PM


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}


Report this thread to moderator Post Follow-up to this message
Old Post
Olaf Schinkel
04-01-08 12:49 AM


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

Report this thread to moderator Post Follow-up to this message
Old Post
huwpioden@gmail.com
04-01-08 01:01 PM


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 
> 
> 
>
> 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.

Report this thread to moderator Post Follow-up to this message
Old Post
C.
04-02-08 12:04 AM


Re: Detect Even Number
..oO(C.)

>On 31 Mar, 14:27, Olaf Schinkel <blabla...@bluxxxxxxxx.de> wrote: 
>
>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

Report this thread to moderator Post Follow-up to this message
Old Post
Michael Fesser
04-02-08 01:05 PM


Re: Detect Even Number
C. wrote on 02/04/2008 01:56: 
> 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.



Report this thread to moderator Post Follow-up to this message
Old Post
Sylvain SF
04-03-08 12:17 AM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PHP Language archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 01:45 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.