Don said:
I have found a little code piece saying:
int i,j;
void main(){
int main() {
i=0; j=0;
i = (i+1) % 32;
j = (j+1) % 2;
}
what does this mean??? What do the % 32 and % 2 do?
By now you should know.
I just want to say that you should perhaps prefer an unsigned int over a
signed int for cycling between 0..(2**N-1). The reason is that some (many?)
compilers may not deduce that this is what the code is intended for and
thereby generate inefficient assembly code, rather than a simpler (and
generally much more efficient) bitmask.
Try disassembling the following functions and you might see what I mean.
int mod32(int x) { return x % 32; }
unsigned mod32u(unsigned x) { return x % 32; }
The first function is likely to be much more costly (slower) than the second
because the compiler must implement the semantics of the % operator, down to
correct results for a negative operand.
Whilst %32 is potentially more descriptive than &31, the results and
efficiency can be affected by the type which the operators are being applied
to.