← Back to Gists

swap ints using XOR

📝 C
mklein
mklein · Level 7 ·

Swap two integer variables in place using bitwise XOR, avoiding a temporary variable.

C
void swap(int x, int y) { if (x != y) { x ^= y; y ^= x; x ^= y; }
}

Comments

No comments yet. Start the discussion.