Friday, April 23, 2010

Modulo 3

Taken from Codercorner..
Thanks to Christer Ericson, one of the fastest way to do:

int Index1 = n; // 0, 1 or 2
int Index2 =
(Index+1)%3;
int Index3 =
(Index+2)%3;

is

int Index1 = n; // 0, 1 or 2
int Index2 = (1 << Index1) & 3;
int Index3 = (1 << Index2) & 3;