//lets assume
a = 10;
b = 20;
a = a^b;
b = a^b;
a = a^b;
// Note: ^ is the XOR operator a = a ^ b b = b ^ a a = a ^ b
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.
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.
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??
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); }
You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;
swap (int *a, int *b) { *a ^= *b; *b ^= *a; *a ^= *b; }
You cannot swap two numbers using call by value, because the called function does not have access to the original copy of the numbers.Swap with call by reference... This routine uses exclusive or swap without temporary variable.void swap (int *a, int *b) {*a ^= *b;*b ^= *a;*a ^= *b;return;}
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 }
To swap two numbers N1 and N2, using a third variable T... T = N1; N1 = N2; N2 = T;
No, you cannot win in Uno by using a Swap Hands card.
You have to pass the address of the variables.void swap (int *pa, int *pb){...}