Avoid using those ifs when you dont need to.
counter = if(below(oldg,g),counter+1,counter);
counter = counter + below(oldg,g);
which is better as it avoids the if which is slower than the + anyway, so dodging the + with if isnt a speed boost.
Whats the & 1 for? you want it to toggle from 0 to 1?
I'm not sure if its faster but I think it is tidier to use:
counter = (counter + below(oldg,g))%2;
I'm not sure but I think & and | are pretty slow 'cos of rounding to int, likewise %, floor etc. The %2 can be replaced by &1 if it is faster...
The other thing is that this can be expanded to 'toggle' between 0,1,2 or 0,1,2,3 and so on, by changing the final %2 for %3 or %4 etc... very useful for complicated menus.
The most comprehensive menu I have released on a preset is on this one:
Although its prolly full of bad coding practice, as all presets are, it shows some of this in use, like the color switchers and click buttons etc.