Click here to Skip to main content
15,879,535 members
Articles / Programming Languages / Java

Swap Two Numbers without using Temp Variable

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
18 Feb 2012CPOL 28.5K   1  
You can do it with some XORs:int a = 25, b = 7;a = a ^ b;b = b ^ a;a = a ^ b;Or the same thing with some shorthand to make the code even harder to read:a ^= b;b ^= a;a ^= b;

Views

Daily Counts

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions