Skip to content

Instantly share code, notes, and snippets.

@automactic
Last active March 21, 2017 13:28
Show Gist options
  • Save automactic/37c72ee3e3f85943a4d07915007d83cd to your computer and use it in GitHub Desktop.
Save automactic/37c72ee3e3f85943a4d07915007d83cd to your computer and use it in GitHub Desktop.
XOR checksum of [0, n]

Input:

val = 0
for i in range(0, 100):
    val ^= i
    print("{}, {}".format(i, val))

Output:

0, 0
1, 1
2, 3
3, 0
4, 4
5, 1
6, 7
7, 0
8, 8
9, 1
10, 11
11, 0
12, 12
13, 1
14, 15
15, 0
16, 16
17, 1
18, 19
19, 0
20, 20
21, 1
22, 23
23, 0
24, 24
25, 1
26, 27
27, 0
28, 28
29, 1
30, 31
31, 0
32, 32
33, 1
34, 35
35, 0
36, 36
37, 1
38, 39
39, 0
40, 40
41, 1
42, 43
43, 0
44, 44
45, 1
46, 47
47, 0
48, 48
49, 1
50, 51
51, 0
52, 52
53, 1
54, 55
55, 0
56, 56
57, 1
58, 59
59, 0
60, 60
61, 1
62, 63
63, 0
64, 64
65, 1
66, 67
67, 0
68, 68
69, 1
70, 71
71, 0
72, 72
73, 1
74, 75
75, 0
76, 76
77, 1
78, 79
79, 0
80, 80
81, 1
82, 83
83, 0
84, 84
85, 1
86, 87
87, 0
88, 88
89, 1
90, 91
91, 0
92, 92
93, 1
94, 95
95, 0
96, 96
97, 1
98, 99
99, 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment