Is there any way to do swapping of two variables in one instruction i.e in one line only ? (Note : weknow that we can swap two variables without using any temporary variable using with 2 methods(XOR and summation method) but that would require three instructions ,but i want solution in only one instruction.
ans:
int main() { int num1 = 10; int num2 = 20; //swap the numbers num1 = num1 + num2 - (num2 = num1); //swapped return 0; }
ans:
int main() { int num1 = 10; int num2 = 20; //swap the numbers num1 = num1 + num2 - (num2 = num1); //swapped return 0; }
copied from a site, check whether its working or not ...