a := a XOR b
b := a XOR b
a := a XOR b
it works, but never use it in real programs
do you know why its not used in real program??
a += b; b -= a; a -= b;
//lets assume a = 10; b = 20; a = a^b; b = a^b; a = a^b;
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
If i is an unsigned char (1 byte unsigned integer) then: i (i << 4) | (i >> 4); If you know how to write inline assembly on your compiler (it differs between compilers), then rotating the byte is much more straightforward way to swap nibbles. In Intel assembly, it would look like: ror i, 4 So in Microsoft compilers it would look like: __asm { ror i,4 }
In general, to swap two variables A and B with each other, you need a third variable T of the same type. You then perform the sequence T = A, A = B, and B = T. For the special case where A and B are binary types, you can swap them without a temporary variable using a bit-wise exclusive or operator. Using C/C++ syntax, you would use the sequence A ^= B, B ^= A, and A ^= B. For this to succeed, the exclusive or operator (^) must be bitwise. Actually, it is also possible to swap any two arbitrarily typed variables, such as float or object, so long as you can properly do the bitwise exclusive or operation, perhaps by doing typcasts or by playing games with pointers - although any of that is potentially dangerous and not always portable.
By using a third temporary variable. $tmp = $a; $a = $b; $b = $tmp;
You can swap two variables, by storing one of them temporarily in a third variable, like this: temp = a; a = b; b = temp; Inside a function, this won't work, because the function parameters are COPIES of the original variables, not the variables themselves. Any change won't affect the original variables. If you work with OBJECTS, and swap the CONTENTS of the objects (not the object pointers), it can work, though.
By using the algorithm of bitwise EORing (Exclusive ORing) the numbers together:If the two numbers are X and Y, then to swap them:X = X EOR YY = Y EOR XX =X EOR Ywill swap them.With knowledge of that algorithm, one then uses the syntax of Javascript to implement it.
How do you do. I am doing well thank you. Swap two number by using reference operators A tough assignment, it will make you think. I think you are confusing reference operators with pointers. Were I you I would study the section on pointers in your text book or course material.
Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);Use a temporary variable. Example (swap variables "a" and "b"): int a = 5; int b = 10; // Swap the variables int temp; temp = a; a = b; b = temp; System.out.println("a = " + a); System.out.println("b = " + b);
a=a^b; b=a^b; a=a^b;
You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;