You can also calculate the XOR-accumulation of all values between 1 and n in O(1) using a lookup table like this:
[n, 1, n+1, 0][n%4]