[Solved] How to XOR n same numbers?


XORing N copies of K together produces K if N is odd and 0 if N is even. K ^ K == 0, 0 ^ K == K, and it just alternates between those results with every additional K. (Your code is currently XORing n+1 copies of k together, which I’m guessing is a mistake.)

int result = (n % 2 == 1) ? k : 0;

solved How to XOR n same numbers?