How do I determine if a number is even or odd?
An even number is any number that can be divided by 2 with no remainder. ColdFusion provides a function, mod, that returns the remainder of a division operation. To determine if a number is even, simply see if the value, modded by 2, returns 0:
If the remainder is 1, then the number is odd.
This question was written by Raymond Camden.
It was last updated on February 3, 2006 at 8:01:44 AM EST.
CFML Referenced
Categories
Comments
Comment made by cedric on February 3, 2006 at 8:37 AM
Math.round(x/2) == x/2
Comment made by darron on February 3, 2006 at 8:51 AM
The mod operator does a lot of processing to get the remainder of a number (multiple subtraction operations). In reality, the processing time is most likely negligible compared to the overall system, but if you want a quicker method try using the BitAnd function:
<cfset x = 5 > <cfif BitAnd( x, 1 ) is 0> The number is even <cfelse> The number is odd </cfif>
BitAnd works on two integer values. It compares the binary values of the integers and does an AND on them based on the following truth table:
a | b | a AND b ~~~~~~~~~~~~~~~ 0 | 0 | 0 0 | 1 | 0 1 | 0 | 0 1 | 1 | 1
Lets take an example. By comparing an integer 5 (binary 0101) to 1 (binary 0001), the result will be the integer 1(0001). Notice how in the result the least significant bit is 1 - the only way a number can be odd is if the least significant bit is 1. Likewise, the result will be zero when the original integer (such as 4, binary 0100) doesn't have the least significant bit set, making it an even value.
Of course, it's better to wrap this inside of a UDF to make the code more understandable. "IsEven( number )" that results in a true/false value is more readable than BitAnd, since not everyone is familiar with bitwise operations.
Comment made by Raymond Camden on February 3, 2006 at 9:34 AM
Very interesting stuff Darron, thanks for sharing.
Comment made by Teddy on February 3, 2006 at 10:20 AM
Bit wise refinement is a great approach to even and odd numbers. I haven't developed any real math applications outside of financials. The determination for even and odd for a newbie CF user would probably be used to determine how to color code a table row from a returned query.
From the UDF perspective, you could place both of the afore mentioned code pieces in a UDF called IsEven() and they would work beautifully and create abstraction bliss.
Comment made by John on February 3, 2006 at 10:35 AM
darron --
Thanks for that info about using bitwise &. I don't work in CF, but I have used the modulus version in ActionScript. I really need to look more into bitwise operators since they are faster.
Comment made by cfJeff on February 3, 2006 at 12:28 PM
That's easier than my test,,, to see if they like Jerry Lewis movies.
Comment made by Peter Tilbrook on February 13, 2006 at 7:26 PM
Or even simpler:
<cfset x = 6>
<cfif x mod 2> The number is odd. <cfelse> The number is even. </cfif>
Comment made by Raymond Camden on February 13, 2006 at 9:11 PM
Peter - I agree your code is simpler, however, one thing I worry about with beginners is short hand format, ie, you are using 0 for false, which is ok, sure, but I'd rather show a beginner a more complete IF clause. (If that makes sense. I know, I'm splitting hairs.)
Comment made by Jamie Price on February 19, 2006 at 7:28 PM
Ah, but is zero even or odd? :-)
Comment made by Marc on January 22, 2009 at 9:43 AM
Even, because it leaves no remainders when divided by 2. i.e. 0/2 = 0 and 0 is a whole number therefore even.