Variable Swap Problem
Someone suggested this problem to me a while back: Swap x and y integers, 6 and 248, without using an extra variable. I was already working on my XOR solution, similar to how RAID 5 and one pad key encryption works, when she told me I could use XOR. I almost had it! But anyway this is the solution I came up with:
[TestMethod]
public void NoExtraVariables_Swap_XY()
{
int x = 6;
int y = 248;
x ^= y;
y ^= x;
x ^= y;
Assert.AreEqual(248, x);
Assert.AreEqual(6, y);
}
[TestMethod]
public void NoExtraVariables_Swap_XY()
{
int x = 6;
int y = 248;
x ^= y;
y ^= x;
x ^= y;
Assert.AreEqual(248, x);
Assert.AreEqual(6, y);
}