oh, it's still doing the circular buffer thing, it's just the way you wrap around the ends of the buffer that i'm talking about, the % that Krzysztof was using.
you have to think about the binary representation of the numbers to understand the bitmasking and it only works with numbers on bit boundaries, ie 2, 4, 8, 16, 32, 64... 256, 512, 1024, 2048...
so, taking 8 as your boundary and using 8 bit representations
0 = 00000000
1 = 00000001
2 = 00000010
3 = 00000011
...
7 = 00000111
8 = 00001000
9 = 00001001
and ANDing (&) with a bitmask means that all the bits that aren't both 1s in the two operators are replaced with 0
so 3 & 0x7 =
3 = 00000011
7 = 00000111
& = 00000011 = 3
and 10 & 0x7 =
Code:
10 = 00001010
7 = 00000111
& = 00000010 = 2
effectively doing %8 without an expensive division. ints are 32 bit in java. this also works going backwards whereas % gives you -ve remainders which won't work as indexes.
some links:
http://en.wikipedia.org/wiki/Bitwise_operation#AND
http://www.darklump.co.uk/blog/?p=295