I hesitated to post on StackOverflow but I think the problem has little to do with programming and more to do with mathematics. So, here it is:
I wanted to compute the function $ f(n) = 0 \oplus 1 \oplus 2 \oplus \dotsb \oplus n$ in O(1) instead of O(n) so I computed $f(1), f(2),...$ to see the global look of the function and it appears that it has an easily identifiable pattern, which can be used to transform $f$ like so:
$$ f(n) = \left\{ \begin{array}{ll} n & \mbox{if } n \equiv 0 \pmod 4 \\ 1 & \mbox{if } n \equiv 1 \pmod 4 \\ n+1 & \mbox{if } n \equiv 2 \pmod 4 \\ 0 & \mbox{if } n \equiv 3 \pmod 4 \\ \end{array} \right. $$
Does anyone know why the function has such pattern ?