Write a C program to input any two numbers from user and swap values of both numbers using bitwise operator. How to swap two number using bitwise operator in C programming.
Example:
Input first number: 22
Input second number: 65
Output first number after swapping: 65
Output second number after swapping: 22
Let's suppose two integer values a and b
And let's suppose x = a ^ b
Then again x ^ b will evaluate to a and x ^ a will evaluate to b.![Swapping two numbers using bitwise XOR operator. Properties of bitwise XOR operator]()
We are going to use this property of bitwise XOR operator to swap two numbers without using third variable.
Happy coding ;)
Example:
Input first number: 22
Input second number: 65
Output first number after swapping: 65
Output second number after swapping: 22
Required knowledge:
Basic C programming, Bitwise operatorLogic:
There are tons of discussions going around the internet about swapping two numbers without using a temporary variable (third variable). Swapping two numbers through bitwise XOR ^ operator is a solution to the problem mentioned above. We know that bitwise XOR operator evaluates each bit of the result to 1 if each corresponding bits of the two operands differ else evaluates to 0. Apart from this property bitwise operator also has an special property which can be explained through an example:Let's suppose two integer values a and b
And let's suppose x = a ^ b
Then again x ^ b will evaluate to a and x ^ a will evaluate to b.
We are going to use this property of bitwise XOR operator to swap two numbers without using third variable.
Program:
/**
* C program to swap two numbers using bitwise operator
*/
#include <stdio.h>
int main()
{
int num1, num2;
//Reads two numbers from user
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
printf("Original value of num1 = %d\n", num1);
printf("Original value of num2 = %d\n", num2);
num1 ^= num2;
num2 ^= num1;
num1 ^= num2;
printf("Num1 after swapping = %d\n", num1);
printf("Num2 after swapping = %d\n", num2);
return 0;
}
Output
Enter any two numbers: 22
65
Original value of num1 = 22
Original value of num2 = 65
Num1 after swapping = 65
Num2 after swapping = 22
65
Original value of num1 = 22
Original value of num2 = 65
Num1 after swapping = 65
Num2 after swapping = 22
Happy coding ;)
You may also like
- Bitwise operator programming exercises index.
- C program to get nth bit of a number.
- C program to set nth bit of a number.
- C program to clear nth bit of a number.
- C program to toggle nth bit of a number.
- C program to get highest set bit of a number.
- C program to get lowest set bit of a number.
- C program to find one's complement of a binary number.
- C program to find two's complement of a binary number.
- C program to flip bits of a binary number using bitwise operator.
- C program to total number of zeros and ones in a binary number.
- C program to convert decimal to binary number system using bitwise operator.
- C program to check whether a number is even or odd using bitwise operator.