java - How to XOR n same numbers? -
i want xor number k n time. easiest way xor n time in loop. there better way?
int t = k; for(int = 0; < n; i++) k = k ^ t;
xoring n copies of k produces k if n odd , 0 if n even. k ^ k == 0, 0 ^ k == k, , alternates between results every additional k. (your code xoring n+1 copies of k together, i'm guessing mistake.)
int result = (n % 2 == 1) ? k : 0;
Comments
Post a Comment