answersLogoWhite

0

Swap of two numbers with xor?

Updated: 12/19/2022
User Avatar

Wiki User

14y ago

Best Answer

void swap (int *a, int *b) {

*a ^= *b;

*b ^= *a;

*a ^= *b;

return;

}

User Avatar

Wiki User

14y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Swap of two numbers with xor?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

How do you swap two numbers with bitwise operator in c plus plus?

// Note: ^ is the XOR operator a = a ^ b b = b ^ a a = a ^ b


How do you swap two variables using bitwise operators?

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??


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;


How you can interchange two integer values using swap variable in c language?

t = a; a = b; b = t; // t is a third integer variable (swap variable) But here's a way without a swap variable, given as as a macro in C: #define SWAP(a,b) { if (a!=b) { a^=b; b^=a; a^=b; }} // Swap macro by XOR Once you define it, you can say swap(x,y) to swap x and y. The numbers kind of flow through each other and end up swapped.


Swapping without third variable?

There are two ways in which you can swap without a third variable. 1. Using xor operation swap( int *a, int *b) { *a = *a ^ *b; *b = *a ^ *b; *a = *a ^ *b; } 2. Using addition and subtraction swap( int *a, int *b) { *a = *a + *b; *b = *a - *b; *a = *a - *b; } }


Write a program to swap two numbers using function?

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


How many xor gates required for 4 bit parity checker?

3*xor- two input


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

void swap(int& a, int& b ) { a^=b^=a^=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;}


Is xor gate is a universal gate?

No, XOR gate is a not a universal gate. There are basically two universal gates NAND and NOR.


How do you write a javascript to swap two numbers without using third one?

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.


What is it called when you swap the two numbers that you multiply?

I don't know.the Switch-A-Roo? not,it's inverse.