# Using BitAnd() To Determine Odd / Even Rows (Thanks Tony Petruzzi)

Posted April 30, 2007 at 2:54 PM by Ben Nadel

Tags: ColdFusion

I recently posted about how the MOD (modulus) operator could be used in ColdFusion to determine even and odd rows of an outputted query. Tony Petruzzi commented that this could also be done using bitwise manipulation and, in particular, the ColdFusion BitAnd() function.

For those of you who don't know how bits come into play when we are talking about numbers, the 10 second run down is that every number is represented by a combination of bits. Of these bits, 8 make up a byte. Each bit can be either 1 or 0 (zero). The order and toggling of these bits determines what numeric value the byte represents. Each bit in the byte represents a "place" in the number. Each place that is turned on (value of 1) is "summed" to get the resultant number.

There's a lot more to it, but anyway, the point here is that Tony Petruzzi raised a good point: using MOD actually requires the ColdFusion server to do a lot of division. Division is more expensive in terms of processing than Bitwise manipulation, which really doesn't require much calculation at all. He recommended performing:

BitAnd( QUERY_ROW, 1 )

This will Bit-AND the "ones" bit with the row number. Since only every other number (even vs. odd) has the ones place turned on, then Bit-ANDing it with one will result in TRUE for every other row. To test this, let's try this:

• <!---
• Loop over the index loop. This could just have
• easily been a query loop where we were using
• --->
• <cfloop
• index="intI"
• from="1"
• to="10"
• step="1">
•
• #intI# :
•
• <cfif BitAnd( intI, 1 )>
• Odd
• <cfelse>
• Even
• </cfif>
•
• </cfloop>

That gives us the following output:

1 : Odd
2 : Even
3 : Odd
4 : Even
5 : Odd
6 : Even
7 : Odd
8 : Even
9 : Odd
10 : Even

This is sneaky little trick. I like it. As he pointed out, you can read more about it over on the ColdFusion Cookbook. I am not sure how the bitwise manipulation happens under the hood, but hopefully it is something that is short-circuited so that the second negative it hits will result in an overall FALSE; if this were the case, it would only ever have to Bit-AND two bits (the "one" column of both numbers).

### Looking For a New Job?

25% of job board revenue is donated to Kiva. Loans that change lives - Find out more »

Apr 30, 2007 at 3:46 PM // reply »

"using MOD actually requires the ColdFusion server to do a lot of division. Division is more expensive in terms of processing than Bitwise manipulation, which really doesn't require much calculation at all. He recommended performing:"

Although I wonder if the compiler is smart enough to turn mod 2 into a check of the "rightmost" bit. I'd like to see some benchmarks, just for curiousity's sake.

Apr 30, 2007 at 3:49 PM // reply »

@Sam,

That's a good point. I assume that the server has to do a lot of division for use with MOD because that is what I do in my head. I wonder how the server optimizes that stuff when it all compiles down. Very interesting.

Apr 30, 2007 at 3:53 PM // reply »

I don't know about anyone else, but interpreting the bitwise operator is a lot less obvious to me compared with a simple MOD operation. When you're programming at the level (with respect to the CPU) of ColdFusion, shaving a few processor instructions is completely folly. First, the language is inherently slow to the point where a couple clock cycles won't mater, and second, the odds of the instructions you anticipate actually being executed as you anticipate are about zero because of all the translation layers that the code has to pass though.

This high up, readability is king, and it should require a pretty significant performance improvement to make a tradeoff that sacrifices readability for performance.

Apr 30, 2007 at 3:55 PM // reply »

@Barney,

Most agreeably. I would still go with the MOD as I find it more readable. But, can't hurt to have more tools in the tool-box.

Apr 30, 2007 at 7:40 PM // reply »

this post brought a tear to my eye ;)

@Sam

Extremely interesting indeed. I wonder if any CF developers at Adobe could tell us that.

@Barney

I agree with you hands down. Although the bitand method is really cool and it is good to have an understanding of bitwise logic under your belt, readability is everything.

@Ben

Now I guess I'm going to be having the same problem that you did. Someone just post a comment to my BASH Friday complaining about one the quotes on BASH :P

May 1, 2007 at 7:04 AM // reply »

Barney - absolutely, readability is the main goal. But, that doesn't mean you couldn't create an isOdd() or isEven() method using that.

Surely its not obvious in the CF world, but bitwise operations are farily common idioms in other languanges (at least it seems to me that I've seen them at least as often as mod).

Ben - And even if CF sends the num mod 2 straight to Java, Java may compile it down to the operation on bits.

May 1, 2007 at 10:37 AM // reply »

Agreed that it's good to remember that we have the ability to do Bitwise calculations in CF.

But, another reason for sticking with MOD: When your boss comes to you and says "Hey, I forgot to tell you. The client wants three different colours for the rows in this table" all you have to do is change the divisor, not your whole alternation method.

May 1, 2007 at 11:31 AM // reply »

@Tom,

Quite true. The MOD stuff would be much easier to change.

Dec 21, 2007 at 3:25 PM // reply »

I agree with you hands down. Although the bitand method is really cool and it is good to have an understanding of bitwise logic under your belt, readability is everything.

Mar 12, 2008 at 5:52 PM // reply »

I did a comparison using GetTickCount() before and after loops over 30,000 rows and sometimes the difference would be 80ms, sometimes 5ms, and sometimes the BitAnd loop would take even longer. Even averaging 10 loops of each showed the same kind of results. I understand the logic of why BitAnd SHOULD take less time, but it really doesn't seem to have much of an effect (at least in my test). Is it possible, during compilation, maybe our MOD 2 statements are simplified into some sort of bit comparison? Anyone else do any tests like this?

Mar 12, 2008 at 5:57 PM // reply »

Since MOD and bit operations are so low-level, the overhead of the loop is going to completely drown out any performance differences between the actual test subject.

Instead, I'd recommend manually copy and pasting your N iterations into a flat file. With compounding copy and paste, you can get a huge number of lines very quickly, and completely eliminate the loop overhead.

In any case, I think you've proven pretty convincingly that any performance different is pretty irrelevant. ;)

Mar 12, 2008 at 5:58 PM // reply »

I guess I should note that this is a server running CF 7 on Windows 2003. I already deleted my test file, but the loops really didn't output anything, just did the even/odd calculation.

Dec 3, 2013 at 2:26 PM // reply »

Fantastic

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

 Author Name: Author Email: Author Website: Comment: Supported HTML tags for formatting: bold   italic   code Remember my information Subscribe to comments Send me a copy of this comment
InVision App - Prototyping Made Beautiful With Prototyping Tools Recent Blog Comments
Mar 11, 2014 at 6:20 PM
Reading In File Data One Line At A Time Using ColdFusion's CFLoop Tag Or Java's LineNumberReader
Thanks Ben for you very interesting articles on handling large files. As I am not a programmer, I am quite insecure whether my issue is directly related to your description or not. My problem is, tha ... read »
Mar 11, 2014 at 12:21 PM
Compound Transclusion Prevented In AngularJS 1.2
Yeah this hit our team too and locked us as 1.1.5. It seemed like an irresponsible unilateral decision, but our graver concern is the future decisions the Angular team will make with other fundamenta ... read »
Mar 11, 2014 at 2:00 AM
ColdFusion, jQuery, And "AJAX" File Upload Demo
Mar 10, 2014 at 8:24 PM
Nested Views, Routing, And Deep Linking With AngularJS
@Steven, the action property is not something from AngularJS but rather part of the concept that Ben is describing in his blogpost here. it is a custom added property, which is read by the requestCo ... read »
Mar 10, 2014 at 2:03 PM
Nested Views, Routing, And Deep Linking With AngularJS
Where is the angular documentation for 'action:' it is not here: http://docs.angularjs.org/api/ngRoute/provider/ \$routeProvider Thanks. ... read »
Mar 10, 2014 at 12:06 PM
Using Track-By With ngRepeat In AngularJS 1.2
I was hoping that this will work with pagination using ng-repeat. My use case scenario is that I have an images object. images[0] = [im1,im2,im3] // First Page images[1] = [im4, im5, im6] // Second ... read »
Mar 9, 2014 at 6:11 PM
For Better Security Use HtmlEditFormat() In Conjunction With JSStringFormat() In ColdFusion
It looks like htmleditformat() will be deprecated in CF 11 https://wikidocs.adobe.com/wiki/display/coldfusionen/New +in+ColdFusion ... read »
Mar 9, 2014 at 10:55 AM
\$.stop() vs. \$.finish() In jQuery Animations
Nice feature! Thanks for sharing. :) Good for when you are making a 100% AJAX controlled site. ... read »