Write a program to swap two variables without using a third variable as an intermediary position.
a=2; b=3;
a=a*b; // 6
b=a/b; // 2
a=a/b; // 3
a=3 and b=2
????is it true,,,i have errors in it
Write a program to swap two variables without using a third variable as an intermediary position.
a=2; b=3;
a=a*b; // 6
b=a/b; // 2
a=a/b; // 3
a=3 and b=2
????is it true,,,i have errors in it
better use reversible operators without "singularities". Such as +
, -
(assuming integer wraparounds are ok.
And, of course, the winner is XOR.
a ^= b;
b ^= a;
a ^= b;
What happens if b or a is zero? If neither is zero and you do not get overflow of the type your code will work.