invictus_07's blog

By invictus_07, history, 6 months ago, In English

XOR is a tiny, ruthless tool. Use it when things cancel, toggle, or hide a single odd element.

1)Quick idea: a ^ b = 1 where bits differ. Key facts: x^x=0, x^0=x, associative.

2)Find unique number: int ans=0; for(x:arr) ans ^= x; → the lonely value.

3)Missing 1..n: xorAll ^ xorArr → missing number.

4)Swap without temp: a^=b; b^=a; a^=b; (don’t use if same variable).

5)Subarray XOR: pref[r+1] ^ pref[l] for O(1) range XOR.

6)Hamming distance: __builtin_popcountll(a ^ b) → differing bits.

7)Toggle flag: flags ^= (1<<i); flips bit i.

8)Gray code: gray = n ^ (n>>1).

When you see “pairs”, “toggles”, or “odd occurrences”, think XOR — fast, memory-light, and contest-ready. ****

Hope this helps many beginners solve more problems confidently.

  • Vote: I like it
  • -3
  • Vote: I do not like it

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

thanks

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

Thanks

»
6 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

helpful :D