Is number even or odd?

# Thread: Is number even or odd?

1. Dave R Guest

## Is number even or odd?

How can I determine within javascript whether a number is even or odd?

2. SteveC Guest

## RE: Is number even or odd?

Look at the first bit of the number... if it&#039s a 1, it&#039s odd. If it&#039s a 0, it&#039s even.

3. Dave R Guest

## RE: Is number even or odd?

I&#039m new at javascript. Could you give me an example? I want to determine if I&#039m dealing with an odd or even row. I have the row number (RowNumber).<BR><BR>Thanks

4. Skippie Guest

## Example

&#060;SCRIPT LANGUAGE="JAVASCRIPT"&#062;<BR><BR> num = 5;<BR><BR> if (num % 2) alert(num + " IS ODD");<BR> else alert(num + " IS EVEN");<BR><BR> num = 6;<BR><BR> if (num % 2) alert(num + " IS ODD");<BR> else alert(num + " IS EVEN");<BR><BR><BR>&#060;/SCRIPT&#062;

5. Dave R Guest

## RE: Example

Thanks!

6. SteveC Guest

## RE: Is number even or odd?

myvar = mynumber & 1;<BR><BR>if(myvar == 1)<BR> {<BR> //I&#039m odd<BR> }<BR>else<BR> {<BR> //I&#039m even<BR> }<BR><BR>Or you can use the modulus that skippie gave; I prefer the bit operator, for no particular reason. Perhaps you can save a few nanoseconds this way?

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## bitwise AND vs. modulo operator...speed?

Or you can use the modulus that skippie gave; I prefer the bit operator, for no particular reason. Perhaps you can save a few nanoseconds this way? <BR><BR>------------------------------------------------------<BR> <BR>If you are using a 486 or older machine, yes. But I think you&#039ll find that Pentiums, and for sure P2&#039s and above, perform the modulus function just as fast as they do an "AND" operation.<BR><BR>Don&#039t have my P2 ref manual here, but I&#039m 90% sure that is correct.<BR><BR>Of course, since it takes JavaScript something on the order of 60 to 100 C++ statements to do *either* of those, odds are good you&#039d have a hard time finding a few nanoseconds difference even if it were there.<BR><BR>

8. Senior Member
Join Date
Dec 1969
Posts
699

## Another answer: Is number even or odd?

Why not embed the calculation, like this. Saves declaring a temp variable.<BR>if(mynumber & 1 == 1)<BR>{<BR>//I&#039m odd<BR>}<BR>else<BR>{<BR>//I&#039m even<BR>}<BR>Bit operations are *always* faster than arithmetic - remember mod has to do a division, which takes ages - bit manipulation takes only a couple of machine cycles.<BR>As Bill mentions, though, you&#039ll have a hard time finding those nanoseconds when the Javascript interpreter is doing so much else.<BR>Pete<BR>

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## Well, if we're looking for cycles...

In point of fact, the fastest thing you can do in JavaScript is simply<BR><BR>if ( theNumber & 1 ) { odd } else { even }<BR><BR>doing an == 1 or == 0 test adds another argument push and an operator execution to the interpreted byte code. (A true compiler would generate the same machine code with or without the comparisons, but JavaScript is far from a true compiler.)<BR><BR>

#### Posting Permissions

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