answersLogoWhite

0

You can swap two integers without temporary storage by bitwise exclusive-or'ing them in a specific sequence...

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

User Avatar

Wiki User

15y ago

What else can I help you with?

Continue Learning about Engineering

Write a C plus plus program to interchange two numbers by using function?

#include<iostream> template<typename _Ty>void swap (_Ty& a, _Ty& b) { _Ty temp = a; a = b; b = temp; } int main() { int x = 42; int y = 0; std::cout << "Before swap:" << std::endl; std::cout << "x = " << x << ", y = " << y << std::endl; swap (x, y); std::cout << "After swap:" << std::endl; std::cout << "x = " << x << ", y = " << y << std::endl; } Example Output Before swap: x = 42, y = 0 After swap: x = 0, y = 42


How do you merge two linked list without using any temporary variable?

You can either insert one list directly into the other, or you can create a completely new list and insert the two lists one after the other. Neither method requires a temporary variable. Temporaries are only required when you need to swap one list for another, but you can minimise this requirement by using "move semantics", where the lists can simply swap their resources, not the elements themselves. In this way, only one temporary is required.


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;


Swap two number using pointer?

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


Swap logic without using third variable?

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

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 write a program swaping of 2 integer?

To swap two integers in a program, you can use a temporary variable. Here's a simple example in Python: a = 5 b = 10 temp = a a = b b = temp Alternatively, you can swap them without a temporary variable using tuple unpacking: a, b = b, a Both methods will effectively swap the values of a and b.


Swap two numbers using call by value and call by reference?

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


During the virtual memory process the operating system moves data into a temporary storage area on the hard drive called?

swap chip


Bubble sorting in c plus plus?

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


Can you swap an engine without having to swap the transmission?

Yes


How to swap two numbers by call by reference in c plus plus?

void swap(int& a, int& b ) { a^=b^=a^=b; }


Write a C plus plus program to interchange two numbers by using function?

#include<iostream> template<typename _Ty>void swap (_Ty& a, _Ty& b) { _Ty temp = a; a = b; b = temp; } int main() { int x = 42; int y = 0; std::cout << "Before swap:" << std::endl; std::cout << "x = " << x << ", y = " << y << std::endl; swap (x, y); std::cout << "After swap:" << std::endl; std::cout << "x = " << x << ", y = " << y << std::endl; } Example Output Before swap: x = 42, y = 0 After swap: x = 0, y = 42


How to write a c progra me to swap two numbers without using a temporary variable?

void main() { int a=2,b=5; b=a+b; a=b-a; b=b-a; getch(); }


Swapping of 2 numbers using java language?

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


How do you swap variables in PHP?

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


How do you merge two linked list without using any temporary variable?

You can either insert one list directly into the other, or you can create a completely new list and insert the two lists one after the other. Neither method requires a temporary variable. Temporaries are only required when you need to swap one list for another, but you can minimise this requirement by using "move semantics", where the lists can simply swap their resources, not the elements themselves. In this way, only one temporary is required.