void swap (int &pa, int &pb) {
*pa ^= *pb;
*pb ^= *pa;
*pa ^= *pb;
}
void swap (int* a, int* b) { if (!a !b) return; // can't swap a pointer to null *a^=*b^=*a^=*b; }
To swap two variables without using a third variable, use exclusive or manipulation... a ^= b; b ^= a; a ^= b;
The only way to swap two values using call by value semantics is to pass pointer variables by value. A pointer is a variable that stores an address. Passing a pointer by value copies the address, the value of the pointer, not the pointer itself. By passing the addresses of the two values to be swapped, you are effectively passing those values by reference. Both C and C++ use pass by value semantics by default, however C++ also has a reference data type to support native pass by reference semantics. By contrast, Java uses pass by reference semantics by default. In C, to swap two variables using pass by value: void swap (int* p, int* q) { int t = *p; *p = *q; *q = t; } In C++, to swap two variables using pass by reference: void swap (int& p, int& q) { std::swap (p, q); } Note that C++ is more efficient because std::swap uses move semantics; there is no temporary variable required to move variables. With copy semantics, a temporary is required. However, with primitive data types, there is a way to swap values without using a temporary, using a chain of exclusive-or assignments: void swap (int* p, int* q) { *p^=*q^=*p^=*q; }
bubble_sort (int N, int *A) { int i; swap = 1; while (swap) { swap = 0; for (i=0; i<N-1; ++i) { if (A[i] > A[i+1]) { swap = 1; A[i] ^= A[i+1]; A[i+1] ^= A[i]; A[i] ^= A[i+1]; } } } }
void swap(int& a, int& b ) { a^=b^=a^=b; }
No, you cannot win in Uno by using a Swap Hands card.
void swap(int &x, int &y) { x ^= y ^= x; } - or - void swap(int &x, int &y) { int t = x; x = y; y = t; }
YES
Although std::swap caters for all built-in types and is specialised to cater for all STL containers, C-style arrays and strings, the default implementation may not be efficient for all user-defined types. Although you can specialise std::swap to cater for user-defined types, this is not considered best practice. The correct way to implement your own swap function is to define it as a global friend function within the same namespace as the object you're swapping so that it can be found with ADL. The following shows an example implementation: class foo : public b1, public b2 { private: int m1; float m2; public: friend void swap (foo& a, foo& b) { // import swap for built-in types using std::swap; // always swap base class members first... swap (a::b1, b::b1); swap (a::b2, b::b2); // ...then swap the members swap (a.m1, b.m1); swap (a.m2, b.m2); } };
You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...a ^= b;b ^= a;a ^= b;
void swap(int &a,int &b) { a=a+b; b=a-b; a=a-b; } int main(void) { int a,b; cout<<"Enter the value for a and b"; cin>>a>>b; cout<<"Before swapping"<<endl; cout<<"A= "<<a<<"B= "<<b; cout<<After swapping"<<endl; swap(a,b); cout<<"A= "<<a<<"B= "<<b; return 0; }
No.