FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Syntax
(Moderators: fry, REAS)
   variable masking and modulo
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: variable masking and modulo  (Read 393 times)
st33d

WWW Email
variable masking and modulo
« on: Mar 2nd, 2005, 8:37pm »

I'm working in C trying to communicate with a micro-controler. I came across a funny statement in the code which was using switch() to swap between eight states on the machine. My first thought was, "so where's the bloody modulo function? How can you just keep incrementing the state with out using modulo?" And that's when I noticed this piece of &= code that I'm unfamiliar with. And I thought - "does it work in Processing?" It does. I'm fairly new to all the wierd bit operations. But what I want to know is- is this how modulo is performed inside the modulo function? Is variable masking faster than modulo? And last but not least, is there a way I can use this variable masking stuff with graphics operations? If anyone has some examples I'd love to know of them. Getting deeper into computing means dealing more and more with the infamous ones and zeros and it's interesting to know how they work.
 
Code:

int state = 0;
void setup(){}
void loop(){
state++;
state &= 0xFF;
//state %= 256;
println (state);
}

 
PS: If in future I'm worried about the speed of a statement does anyone know of some code to time the execution speed? It would mean I wouldn't have to post the board with speed questions anymore (I know speed is relative to machine - but a test mechanism sure would help me sleep better on some code jobs (the mechanism would have to acount for its own execution time too - tricky)).
« Last Edit: Mar 2nd, 2005, 8:52pm by st33d »  

I could murder a pint.
Quasimondo

WWW Email
Re: variable masking and modulo
« Reply #1 on: Mar 2nd, 2005, 9:45pm »

I didn't use a timer to proof it but I guess that a bitwise operation aka "&" is faster than modulo. The problem is that it's not as versatile. You can only use it in steps that are the power of 2 minus 1:
 
1,3,7,15,31,63,127,255,511....
 
So in your case it works fine, because you have 8 bits = 255
 
As for graphics operations - it might be useful in clipping coordinates to the allowed range - but you would have to choose an imagesize from the numbers in the list above.
 
As for timing functions:
 
int startTime=millis();
doLotsOfCalculations();
int runTime=millis()-startTime;
« Last Edit: Mar 2nd, 2005, 9:46pm by Quasimondo »  

Mario Klingemann | Quasimondo | Incubator | côdeazur
fry


WWW
Re: variable masking and modulo
« Reply #2 on: Mar 3rd, 2005, 2:02am »

in a microcontroller, bit operations will be much faster. also, switch statements, perhaps counter-intuitively, are quite fast as compared to an if() function, because the compiler internally builds a table of states that makes the transitions nice and speedy.
 
in a "real" microprocessor, i believe module is probably an operation just like division. so it's straightforward, but just as slow as a divide. the difference in speed between it and using bit operations is less than in a microcontroller, but probably still exists.
 
in processing, you're even further away from the "metal", and java will likely give you little or no speedup from bit operations versus modulo, because there's so much overhead with everything that happens around it. i didn't test it either, but past experience has been that bit operations provide speedup in some cases but are nonexistant in others. for instance, a bit operation like this in c:
 
if (a & 3)  // tests to see if the low 2 bits are set
 
looks like this in processing:
 
if ((a & 3) != 0)  
 
and offers very little speedup, because it's not tied to a direct processor operation the c version is.
 
which is perhaps a long way of saying that java is as un-microcontroller as you can get, so "good" practices that you learn from it may lead to confusing code with negligible speed improvement. your mileage *will* vary.  
 
Pages: 1 

« Previous topic | Next topic »