Bitwise or bit stupid?

I’ll hold my hand up right now and confess to being bit stupid, I was really disappointed with my score in a recent php proficiency test and most of my downfall was bitwise operators (the rest was array functions I’d never needed to use before and I’ll tackle that in another post).

So what’s all the fuss about?

I’ve never learnt about bitwise operators because I’d never seen a practical use. I knew of their existence in a vague etherial sense but thought they were historical like other direct interaction with binary – punchcards or machine code for instance. Maybe they might be needed in embedded systems – not something I’d ever thought of getting into. PHP (personal home page as it started out) is after all a web coding language.

Luckily I know one or two c++ software engineers (including the Other Half) who were patient enough to explain bitwise operators in clearer terms than the php manual. The practical uses are still fairly limited in my view so please point out others to me in the comments if you feel kind or want to put me straight.

Case Study: Checking whether an iterator is odd or even:

So you want to make your data table rows alternate between two styles?
As there is no is_odd() function built into php, before being bitwise I would have used something like: –

  1. <table>
  2. <?php
  3. for ($i=0;$<=$tableRows;$++) {
  4. if (!is_int($i/2)) {?>
  5. <tr class="row1">
  6. <?php
  7. } else {?>
  8. <tr class="row2">
  9. <?php }
  10. ?>
  11. </table>

Now I know I can check the least significant bit to see if it is set or not. This will tell me if $i is odd or even by using following code in line 4 above:

if ($i & 1) {?>

Two days ago I would have said huh? so here’s a bit of explanation:

Say at a point in the loop $i = 7. This would make our line 4 equivalent to if (7 & 1)
& is the bitwise ‘and’ operator and my binary logic is still a little sketchy but there is a nice clear entry on bitwise operation on wikipedia to help you understand how we achieved the numbers in the following table. Lets have an example:

Integer 4th bit (8’s) 3rd bit (4’s) 2nd bit (2’s) 1st bit (1’s)
First arg 7 0 1 1 1
Second arg 1 0 0 0 1
Answer 1 (TRUE = is odd) 0 0 0 1

I’ve done a timing test and here is the concluding output:

conclusion: using bitwise operators take longer

is_int method: 8.29696655273E-5 secs

bitwise method: 0.000131130218506 secs

So it definitely works just the same and does not need any extra code. However, as part of a local lamp environment and in this case, the bitwise operation code doesn’t take less time as advertised but more! I’m sure in a compiled solution it would make a big difference but not on the average website.
Don’t take my word for it though, test it on your own system.

Perhaps there is a more complicated problem out there that bitwise operations can solve more efficiently?

One thought on “Bitwise or bit stupid?”

  1. When it comes to testing if something is odd/even I would use the modulo operator so that the test is:

    if ($i %2) {
    // even
    } else {
    // odd

    As for bitwise operators, I use it for flags in a single integer. Flag1 = 1, flag2 = 2, flag=4, flag4=8 etc. you can then use If(($var & Flag1 == flag1) to test for the existance of the flag within $var.



Comments are closed.