you can do it in the following manner
Supposing your two variables are x and y:
int x=3;
int y=5;
x=x+y; [x becomes 8]
y=x-y; [y assumes the original value of x i.e. 3]
x=x-y; [x assumes the original value of y i.e. 5]
or... you can use the unary XOR (exclusive or) operator, '^=' .
Same values, int x=3, y=5
x ^= y; // x becomes 6
y ^= x; // y becomes 5
x ^= y; // x becomes 3.
The second method has more advantages :
- Its assembler operations never use the processor's ALU carry.
- Without use of the aforementioned carry, no overflow will ever occur.
- Performing this with 32-bits values 8-bits processors will be more efficient, in terms of program space AND execution speed.
- You can even use this in Visual Basic an other languages which implements boundaries on values, while the first method is guaranteed to fail when overflows occurs.
In both case, do not EVER try to factorize these 3 lines into two, as the operations order in multiple-operators lines depends on the compiler's way to parse your code.
Thus , typing
x = x ^ y ^x;
y = y ^ x;
Will surely give you garbage.
#include<stdio.h>
#include<conio.h>
int main (void)
{
int a = 5;
int b = 8;
printf ( " Before swapping a = %d , b = %d\n",a,b);
swap(&a, &b);
printf ( " After calling swap function a = %d , b = %d\n",a,b);
getch();
return 0;
}
void swap(int* const p , int* const q)
{
int temp = *p;
*p = *q;
*q = temp;
}
Use the following template function to swap any two objects without using a temporary object:
template<class T> void swap(T& x, T& y){x^=y^=x^=y;}
Note that all objects passed to the function must provide an overload for the bitwise XOR assignment operator (^=) in order for this to work. However, primitive types such as int and char can be passed to this template function without the need for any additional code.
#include<iostream>
void swap(int& x, int& y)
{
x^=y^=x^=y;
}
int main()
{
int a=1, b=2;
std::cout<<"a="<<a<<", b="<<b<<std::endl;
swap(a,b);
std::cout<<"a="<<a<<", b="<<b<<std::endl;
return(0);
}
int main()
{
int a = 13, b = 29, c = 237
a ^= b; // a == 16
b ^= a; // b == 13
a ^= b; // a == 29
// You could stop here, since 'a' and 'b' values are now swapped, or you can continue with 'c' and 'b', as required.
c ^= b; // c == 224
b ^= c; // b == 237
c ^= b; // c == 13;
/* In short, 'a' got the previous value of 'b' , 'c' the value of 'b', and 'b' the value of 'c'.
To make it simpler
#define SWAP(X,Y) {X^=Y; Y^=X; X^=Y}
int main(void)
{
int a=37, b=32766;
SWAP(a,b); // nifty macro !
}
Please note the following coding for the same if the question is How do you write a program in C to swap two variables without using another variables? :-
void main()
{
int a,b;
printf("Enter a and b:", a,b);
scanf("%d %d",&a,&b);
a=a+b;
b=a-b;
a=a-b;
printf(" a=%d and b=%d",a,b);
}
By: Anil Kothiyal
void swap(int* x, int*y)
{
(*x)^=(*y)^=(*x)^=(*y);
}
to swap a and b we can use the following lines of code:
a=a+b;
b=a-b;
a=a-b;
a=6,b=5
a=a+b;/*11*/
b=a-b;/*11-5=6*/
a=a-b;/*11-6=5*/
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }
A = A xor B B = A xor B A = A xor B in C... A^=B; B^=A; A^=B;
a ^= b; b ^= a; a ^= b;
swap (int *pa, int *pb) { *pa ^= *pb; *pa ^= *pa; *pa ^= *pb; }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)
the third variable problem is when an unintentional third variable influences two separate variables that are being measured. this causes a random and coincidental relationship between the two variables. An example would be as ice cream consumption goes up the number of drownings also goes up. the unintentional third variable in this case would be the heat.
Yes. int a, b; a= 2; b= 3; a= a+b;
a=a^b; b=a^b; a=a^b;
Moderation occurs when the relationship between two variable depends on a third variable. The third variable is referred to as the moderate variable or simply the moderator
By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;
#include <stdio.h> int main (void) { char *first= "Hello"; int second = 12; printf ("first=%s, second=%d\n", first, second); return 0; }
The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }
Consider the following declarations:int x = 0;int y = 1;In order to swap the values, we need to use a temporary variable:int t = x;x = y;y = t;However, it is possible to swap the values without using a third variable:x ^= y ^= x ^= y;
A = A xor B B = A xor B A = A xor B in C... A^=B; B^=A; A^=B;
Assuming that x and y are two variables, such as they might be in Algebra, and that the expression "xy" is meant to be the multiplication of x times y, you would write it as x * y This expression could be used with a third variable in an assignment statement, or as a condition in an if statement where it can be compared to another variable or a constant.