65.9K
CodeProject is changing. Read more.
Home

Swap Two Numbers without using Temp Variable

starIconstarIconstarIconstarIconstarIcon

5.00/5 (4 votes)

Feb 19, 2012

CPOL
viewsIcon

29693

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;

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;