And On The Seventh Row, MOD Created 1, And It Was Good

On Ray's Friday Puzzler, it came up that not everyone knows what the MOD (or Modulus) operator is in math. This is one of those things that once you know it, you can't imagine how you lived with out it. The MOD operator does a fairly simple thing: it divides two numbers and returns only the remainder.

To demonstrate how this works in a practical sense, let's run this code:

Notice that we can see a sequence coming through: 1,2,0. The sequence is as long as the number we are using to MOD. This is because every Nth value should be evenly divisible by (N) and then the remainder gets "reset."

This can be a headache saver for ColdFusion query output when you want to format alternating rows. To figure out which row you are on (odd vs. even), all you have to do is MOD the current row by 2. If 2 divides evenly into the row number (and thereby resulting in a remainder of zero), we are on an even row. If we MOD by 2 and have a remainder, we must be on an odd row.

Tons of stuff has been written about the Modulus operator. It's totally awesome. When working on Ray's puzzler, I was having a huge mental block trying to figure out how to get rid of the zero in my sequence. Here is a great resource I found that talks about using and manipulating sequences using the modulus operator.

Reader Comments

Btw, I never got to comment on your issues with being a senior programmer. I am not sure what that means given my being self employed or working for interweb startups and small dev companies for the last 10 years. But I have been reading your blog for the last 4-5 months and I can truthfully say that you are a damn good programmer. You have taught me quite a bit.

Another handy use: creating a specific number of columns. I use this all the time since I find myself creating "sets" of checkboxes. If I have 11 checkboxes it's easier to read if they're laid out so that there are 2 rows of 4 and one row of 3.

To do that, I usually give the parent element a pre-defined width and float it to the left. Each successive checkbox then is added to a single row. I can then just test the current index MOD 4 (current index % 4 in php and others) and, if the value is 1, set the element class to "startrow" or some such. To that class I apply clear: left to force the creation of a new row.

That is very slick! Bit manipulation is one of those dark arts that I think very few people (including myself) really understand how to take advantage of. I have used Bits as a way to group "flags" for objects, but nothing like this.

Thanks for the awesome tip. And yes, I can see this would be a much better performance feature (as division never actually needs to take place). Cool!

As far as this method being faster, I suppose it would be because it's not actually performing any mathematical dvision (as would be required for MOD). Really, we are just taking the binary representation of the number and examining the first digit. I assume that this would be a constant speed no matter how large the number. Thanks for the good tip.

I was trying to get it to execute on the third iteration. What I found is doing x MOD y = 0, where y is your desired iteration (in my case 3) seems to work in any case. Run the code and see for yourself!

I'm referring to using MOD in a cfoutput query, where you'd need every other row highlighting, or a line break after every third item, or fifth. I've found the easiest and most legible way is to do :currentRow MOD 2 EQ 0 orcurrentRow MOD 3 EQ 0or currentRow MOD 5 EQ 0 respectively.

The only need I've had for MOD thus far has been in a query loop, so I guess it's a narrow perspective.