devxlogo

Swapping a Temporary Variable

Swapping a Temporary Variable

 swap(int & a, int & b){a ^= b;b ^= a;a ^= b;}

?
The trick lies in the bitwise xor (^) operator. The compiler doesn’t generate a temporary variable for this operation.

devx-admin

Share the Post: