A bitwise operation is an operation that works on the individual bits of a number. Yes, that means the binary(base 2) representation of a number. These bitwise operations work by exploiting properties of binary numbers to their advantage. For instance, if the binary representation of a number ends with a 1, it’s odd.

## Base 2 Refresher

For those of us who don’t manipulate bits every day (including myself), I thought that a quick refresher on binary representation would be a good idea. First, let’s create a table.

4 |
3 |
2 |
1 |
0 |

0 | 0 | 0 | 0 | 1 |

Each column in the table represents a power of 2. The first column (from the right) represents 2^{0}, the second 2^{1}, and so on. Notice that I have a 1 in the row below the 0 column. That stands for 2^{0}, and 2^{0} is equal to 1. Therefore, the number represented above (00001) is equal to 1. On to the next table.

4 |
3 |
2 |
1 |
0 |

0 | 0 | 1 | 1 | 1 |

^{2}+ 2

^{1}+ 2

^{0}, or 4 + 2 + 1, which is equal to 7. As you can see, using binary representation is pretty easy. On to our final example.

4 |
3 |
2 |
1 |
0 |

1 | 1 | 0 | 0 | 1 |

^{4}+ 2

^{3 }+2

^{0}(16 + 8 + 1) = 25. Notice if there are zeros in the columns, we just ignore them completely.

## Trick 1 : Odd / Even

One of the most useful bitwise tricks for web developers is for determining if a number is even or odd.

for($i = 0; $i < 10; $i++) { if($i & 1) { echo "This is odd."; } else { echo "This is even."; } } |

## Trick 2 : Multiplication and Division

//Example 1 $x = 2; echo $x << 1; //4 echo $x << 2; //8 //Example 2 $x = 16; echo $x >> 1; //8 echo $x >> 2; //4 |

In example 1, we left shift the number 2 one place. By left shifting 1 place, it’s the same as multiplying that number by 2. By left shifting 2 places, it’s the same multiplying the number by 2 twice.

Example 2 is doing bitwise division. Right shifting by 1 place is the same as dividing the number by 2. Right shifting by 2 places, is the same is the same as dividing the number by 2 twice.

## Others

There are most definitely a ton of other uses for bitwise operators, however these are the ones I use the most. If you happen to have a favorite, please let everyone know about it in the comments.

For a more in depth introduction to bitwise operations please check out http://en.wikipedia.org/wiki/Bitwise_operation and http://www.litfuel.net/tutorials/bitwise.htm

“In example 1, we are left shift the base”

Good catch. I edited it for clarity.

Could you give some other examples please? What would be the benefit of using Trick 1 with & 1 instead % 2? Why would I would << 2 in Trick 2 when I could use *4 and so on. I've been doing PHP for 10 years now and the only time I really used bitwise operators if for bitmasks, which no one uses nowadays anymore.

Generally speaking, the only time bitwise operations come out to play is when you need to optimize things. For instance, http://lab.polygonal.de/2007/05/10/bitwise-gems-fast-integer-math/ has some good benchmarks with ActionScript 3. Most of these benchmarks apply to PHP’s use of bitwise operators as well.

Jack, thank you. I have to admit, I had hoped for something more practical though. While bitwise operations are indeed somewhat faster, the difference is totally and utterly neglectable unless you are doing millions of computations. This is even less measurable than the infamous single quotes versus double quotes “optimization”.

I’d say the majority of programmers of PHP are not familiar with bitwise operations, so my suggestion would be not to confuse people with the bitwise solution just to shed some nanoseconds.

That’s not to say the article is bad. It’s good. I think it’s mentally stimulating and enjoyable. It’s just not that practical.

Learn those operations event if you don’t plan to use them immediately. That are often useful.