def g(v): r = 0 while v%4!=3: r ^= v v -= 1 return r def f(k,L,d): r = 0 v = L>>d # v ^ v+1 ^ ... ^ v+k-1 r ^= g(v+k-1)^g(max(v-1,0)) return (r<