answersLogoWhite

0

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;



User Avatar

Wiki User

12y ago

What else can I help you with?

Related Questions

How do you write a program in C plus plus plus plus How do you write a program in C to swap two variables without using the third oneo swap two variables without using the third one?

To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;


How do you swap variables in PHP?

By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;


What are the advantages of using pointers in a program?

pointers points to the memory address of another variable.....in functions we have two kind of variables the actual and dummy variable. when we operate on variables..the value of dummy variables are effected, but if we want to make changes in the actual variable then we have to refer to their address..and we can reach to address of the variables by only using pointers.


What are register variables What are the advantage of using register variables?

Asks the compiler to devote a processor register to this variable in order to speed the program's execution. The compiler may not comply and the variable looses it contents and identity when the function it which it is defined terminates.


Can you give an example of array using character variable in c program?

the example of array over charcter variables is char ["string"]


How do you write a program in C to swap two variables without using the third one using XOR?

a=a^b; b=a^b; a=a^b;


How do you write a program in Perl to swap two variables without using the third one?

Use list assignment i.e. for two variables $a, $b: ($a,$b) = ($b,$a)


What is variables in c languages?

Variables are means for location in memory used by a program to store data. The size of that block depends upon the range over which the variable is allowed to vary. For example, on personal computer the size of an integer variable is two bytes, and that of a long integer is four bytes. A variable region is temporarily remember a number or string value, such as covered by the program. To identify the variables, you have a name unique to every single variable. This is called a variable name. Before using a variable, use variables to what is called a variable declaration that you have to reveal the names and data types that can be stored in the variable variable.


Can you write addition of 2 no program without using third variable?

Yes. int a, b; a= 2; b= 3; a= a+b;


What is the difference between controlling variables and using controls?

Controlling variables is when you make sure that only one variable is being tested at a time and that there are not other variables that will make your results unclear. Using a control is when you do a trial without the variable to see what the normal results are.


What is the Algorithm for exchanging values of two variables?

There are three primary algorithms to exchange the values of two variables. Exchange with Temporary Variable temp = a; a = b; b = temp; Exchange Without Temporary Variable Using Exclusive Or a = a ^ b; b = b ^ a; a = a ^ b; Exchange Without Temporary Variable Using Arithmetic a = a + b; b = b - a; a = a - b;


How can you devise a way to interchange two variables A and B without using a third variable or register using Exclusive OR?

A = A xor B B = A xor B A = A xor B in C... A^=B; B^=A; A^=B;