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

Popular posts from this blog

cookies - Yii2 Advanced - Share session between frontend and mainsite (duplicate of frontend for www) -

angular - password and confirm password field validation angular2 reactive forms -

php - Permission denied. Laravel linux server -