answersLogoWhite

0

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.

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;


Third variable problem?

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.


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)


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

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


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;


What is moderating variable?

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


How do you swap variables in PHP?

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


Write a program for two variables without using third variable in c plus plus?

#include <stdio.h> int main (void) { char *first= "Hello"; int second = 12; printf ("first=%s, second=%d\n", first, second); return 0; }


What is program in c to swap entered two numbers without using third variable?

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); }


What is Situation-Relevant Confounding Variable?

A situation-relevant confounding variable is a third variable that is related to both the independent and dependent variables being studied, which can lead to a spurious relationship between them. It is crucial to identify and control for situation-relevant confounding variables in research to ensure that the true relationship between the variables of interest is accurately captured.


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

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;


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;