A common programming task is to determine if an integer is even or odd. Recently, I saw an article showing how to do the task faster than the usual way: (i % 2) == 0. Today’s article shows an even faster way to check for even or odd.

The usual way is simple and most people know it:

(i % 2) == 0

The way suggested by the article is much more complex:

((i * 0.5) - (i >> 1)) == 0

The way I’m proposing is both simple and, as you’ll see, even faster:

(i & 1) == 0

The way this works is to check only the singles digit. Here’s an alternate that works because only 0 is false:

!(i & 1)

To find out which is the fastest, let’s put them through a little performance test:

package
{
	import flash.display.*;
	import flash.utils.*;
	import flash.text.*;
 
	public class FasterIsEven extends Sprite
	{
		private var __logger:TextField = new TextField();
		private function row(...cols): void
		{
			__logger.appendText(cols.join(",")+"\n");
		}
 
		public function FasterIsEven()
		{
			stage.align = StageAlign.TOP_LEFT;
			stage.scaleMode = StageScaleMode.NO_SCALE;
 
			__logger.autoSize = TextFieldAutoSize.LEFT;
			addChild(__logger);
 
			init();
		}
 
		private function init(): void
		{
			var beforeTime:int;
			var afterTime:int;
			var i:int;
			var REPS:int = 1000000000;
			var even:Boolean;
 
			row("Method", "Time");
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				even = (i % 2) == 0;
			}
			afterTime = getTimer();
			row("(i % 2) == 0", (afterTime-beforeTime), even);
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				even = ((i * 0.5) - (i >> 1)) == 0;
			}
			afterTime = getTimer();
			row("((i * 0.5) - (i >> 1)) == 0", (afterTime-beforeTime), even);
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				even = (i & 1) == 0;
			}
			afterTime = getTimer();
			row("(i & 1) == 1", (afterTime-beforeTime), even);
 
			beforeTime = getTimer();
			for (i = 0; i < REPS; ++i)
			{
				even = !(i & 1);
			}
			afterTime = getTimer();
			row("!(i & 1)", (afterTime-beforeTime), even);
		}
	}
}

I ran this test app in the following environment:

  • Flex SDK (MXMLC) 4.6.0.23201, compiling in release mode (no debugging or verbose stack traces)
  • Release version of Flash Player 11.3.300.268
  • 2.3 Ghz Intel Core i7
  • Mac OS X 10.8.0

And here are the results I got:

Method Time
(i % 2) == 0 10106
((i * 0.5) – (i >> 1)) == 0 2974
(i & 1) == 1 2277
!(i & 1) 2612

Faster isEven Performance

We see some interesting results here:

  • The naive method ((i%2) == 0) is the slowest by about 3x!
  • The method suggested in the article is much quicker
  • My suggested method is the fastest by a decent margin
  • My alternative method (!(i & 1)) is slower

So if you want a fast isEven check, use my method: (i&1)==0. It’s not complex and it’s quite a bit quicker than the usual approach. Also, you can make it into an isOdd very easily: (i&1)==1.

Spot a bug? Have a question or suggestion? Post a comment!