代碼如下:
#include <stdio.h>
int main (void)
{
unsigned int MAX = 32;
unsigned int index = 31;
index = 31;
index = (index + 1) % MAX; // 這個容易理解
printf ("index = %d\n", index);
index = 31;
index = (index + 1) & (MAX - 1); // 這個運算效率高
printf ("index = %d\n", index);
return;
}
$ ./a.exe
index = 0
index = 0