Alternating table row styles in PHP - strange usage of bitwise operator

Viewed 177

Looking at some code written by another developer, I came across this:

for($i=1; $i<=30; $i++)
{
  if($i&1)
    $color = '#fff';
  else
    $color = '#bbb';
}

This $color variable is used for row background colour later in the code. The alternating colours work fine.

If I was writing this, I would have used the modulus operator (%) rather than the bitwise (&) operator.

Why does the bitwise operator work in this case? Is there any advantage of using this method rather than the modulus operator?

2 Answers
Related