The xor basis is a technique for problems about the xor sum of subarrays (contiguous or not) of an array of integers, or about appending elements to an array while answering queries.
The technique splits into two main parts:
Represent the integers in base 2 and view each element as a vector in the vector space Z2d, where d is the number of bits needed to represent them. Xor between elements then equals addition between the corresponding vectors in Z2d.
Relate what a query asks to the basis of the vector space built above.
Some concepts
Vector space
A set V=∅ is a vector space if V carries two operations: addition and multiplication by a scalar. The elements of V are called vectors.
V is closed: ∀x,y∈V⟹x+y∈V and ∀x∈V⟹kx∈V, where k is a scalar.
Addition is associative and commutative.
Multiplication by a scalar is associative and distributive.
V contains an element O called the zero vector (not to be confused with the number 0).
Some special vector spaces:
The coordinate space Rn={(x1,x2,…,xn)}.
The vector space Mn×m of n×m matrices.
The vector space Pn(x) of polynomials of degree at most n.
The vector space F of functions.
…
Linear independence and dependence
A set of vectors {v1,v2,…,vn} is linearly independent if the equation
x1v1+x2v2+…+xnvn=O
has only the trivial solution x1=x2=…=xn=0.
Conversely, if there exist x1,x2,…,xn not all zero with
x1v1+x2v2+…+xnvn=O,
the set is linearly dependent.
The vector space Z2d
Z2: Zm is the set of residues when dividing by m, and the operations on Zm also divide by m. So Z2 is the set of residues when dividing by 2, hence Z2={0,1}.
Z2d: a d-dimensional vector space containing every vector with d coordinates, each coordinate an element of Z2.
For two vectors x,y∈Z2d, define x+y as x⊕y (⊕ is bitwise xor). Note that x+y∈Z2d must hold. Concretely:
⊕
Sum
Sum mod 2
0
0
0
0
0
0
1
1
1
1
1
0
1
1
1
1
1
0
2
0
For c∈Z2, write cx=c times xx+x+…+x. Then cx=0 if c is even and cx=x if c is odd.
From here on we only care about the vector space Z2d. Every computation is taken modulo 2, and we treat ⊕ (bitwise xor) as the + of Z2d.
Linear span
A vector space V is spanned by a set of vectors S={v1,v2,…,vn} if it contains every vector x expressible as a linear combination of the vectors in S.
span(S)={∑i=1ncivi∣vi∈S,ci∈{0,1}}
Then we say V is spanned by S, V is generated by S, or S generates V.
Note: since we only work in Z2d, each coefficient ci is also a binary value. So in the combination above each vi has exactly two states: present or absent.
Example: find span({2,5}), span({2,5,7}) and span({})?
Since 2⊕5=7:
span({2,5})=span({2,5,7})={0,2,5,7}span({})={0}
Some important properties:
If vn+1∈span({v1,v2,…,vn}) then:
span({v1,v2,…,vn,vn+1})=span({v1,v2,…,vn})
span({v1+v3,v2,…,vn})=span({v1,v2,…,vn})
Basis
A set of vectors B={v1,v2,…,vn} is a basis of a vector space V if span(B)=V and B is linearly independent. Then n is the dimension of V, written dim(V).
Example: consider the vector space V={0,2,5,7}. {2,5} is a basis of V, but {2,5,7} is not, because it is linearly dependent: 2⊕5⊕7=0. Naturally {2,7} and {5,7} are also bases of V.
Note: the number of distinct elements in a vector space V spanned by a basis B={v1,v2,…,vn} is ∣V∣=2n.