So, telling in steps: We calculate the maximum possible xor bit by bit here. Sort all the numbers(we are doing this because we want the maximum xor and the largest number is likely to have the highest possible bit set we find that in lines 29-30 of the ideone code). Now to include this number(let this number has ith bit as its MSB), all other numbers having ith bit set should not contribute as otherwise the result will be the xor of the two i.e. 0. What I mean is let there are two numbers 7 and 6. Both have 3rd bit set, if we include 7, we cannot have 6 simultaneoulsy as otherwise 3rd bit in the answer will then be 0. This is what we are checking in lines 46-50. If any number has that bit set, we zero out its contribution by doing xor. As you can see:
for(ll row=0; msb>=1; msb>>=1)
msb here denotes the next set bit or ith for first iteration or (i-1)th bit for 2nd iteration in example you can say. If no number is found with that msb set, we move on to the next msb(lines 41-42). We check in lines 38-39, which number has that bit set. Once we get that number at index temp,
The lines just simply stores the result in sorted order and then again we want all other numbers should not have this bit set and thus, lines 46-50. The resultant vector will then have the corresponding bits which result to maximum xor. In the matrix method, you find rank of the matrix. We try reducing it by making rows of the matrix zero by applying operations like addition/subtraction/multiplication. This is exactly what we doing here with operation as xor.
Lets say I have three numbers as 3,10,9. After sorting they become 10
(0011). MSB is the fourth bit. We store this number as our first result and xor with all other which have the same msb set. Here, we xor with 9 and
vec becomes 3. The vector now contains
(10,3,3). Now repeat the same with the next msb. MSB now is 4, and no number has that bit set. Thus, we move to next msb which is 2 and the value of row variable here is 1. Doing similar in this iteration, we get vector as
(9,3,3) see in the output of the code.
Write binary representation of some numbers and try manually. Read this.
Hope it clears.