To toggle bit (flip 0 to 1 or 1 to 0) in number , you XOR with . In code: .
XOR flips bits because and . When you XOR with a 1, that bit flips. When you XOR with a 0, the bit stays the same.
Example: Toggle bit 1 in . Compute . Bit 1 flipped from 0 to 1. Toggle again: . It's back.