Problem Statement:- Program to swap two numbers using bitwise operators.
Sample Input/Output:-
Data requirement:-
Input Data:- num1, num2
Output Data:-num1, num2
Program in C
Here is the source code of the C Program to swap two numbers using bitwise operators.
Code:
#include <stdio.h>int main(){ int num1, num2; /* Get the two numbers input */ printf("Enter the two Numbers:"); scanf("%d %d", &num1, &num2); /*Swaping two numbers using bitwise XOR operators.*/ num1 = num1 ^ num2; /* ^(XOR operators) */ num2 = num1 ^ num2; num1 = num1 ^ num2; printf("After Swapping two Numbers are:\n"); printf("Number1=%d\nNumber2=%d\n", num1, num2);
return 0;}
Input/Output:
Enter the two Numbers:17 21
After Swapping two Numbers are:
Number1=21
Number2=17
#include <stdio.h>int main(){ int num1, num2; /* Get the two numbers input */ printf("Enter the two Numbers:"); scanf("%d %d", &num1, &num2); /*Swaping two numbers using bitwise XOR operators.*/ num1 = num1 ^ num2; /* ^(XOR operators) */ num2 = num1 ^ num2; num1 = num1 ^ num2; printf("After Swapping two Numbers are:\n"); printf("Number1=%d\nNumber2=%d\n", num1, num2);
return 0;}
Program in C++
Here is the source code of the C++ Program to swap two numbers using bitwise operators.
Code:
#include <iostream>using namespace std;int main(){ int num1, num2; /* Get the two numbers input */ cout << "Enter the two Numbers:"; cin >> num1 >> num2; /*Swaping two numbers using bitwise XOR operators.*/ num1 = num1 ^ num2; /* ^(XOR operators) */ num2 = num1 ^ num2; num1 = num1 ^ num2; cout << "After Swapping two Numbers are:\n"; cout << "Number1= " << num1 << "\nNumber2= " << num2; return 0;}
#include <iostream>using namespace std;int main(){ int num1, num2; /* Get the two numbers input */ cout << "Enter the two Numbers:"; cin >> num1 >> num2; /*Swaping two numbers using bitwise XOR operators.*/ num1 = num1 ^ num2; /* ^(XOR operators) */ num2 = num1 ^ num2; num1 = num1 ^ num2; cout << "After Swapping two Numbers are:\n"; cout << "Number1= " << num1 << "\nNumber2= " << num2; return 0;}
Input/Output:
Enter the two Numbers:33 72
After Swapping two Numbers are:
Number1= 72
Number2= 33
Program in Java
Here is the source code of the Java Program to swap two numbers using bitwise operators.
Code:
import java.util.Scanner;
public class SwapTwoNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* Get the two numbers input */ System.out.print("Enter the Two Numbers:"); int num1 = sc.nextInt(); int num2 = sc.nextInt(); /* Swaping two numbers using bitwise XOR operators. */ num1 = num1 ^ num2; /* ^(XOR operators) */ num2 = num1 ^ num2; num1 = num1 ^ num2; System.out.print("After Swapping two Numbers are:\nNumber1= " + num1 + "\nNumber2= " + num2); sc.close(); }}
Input/Output:
Enter the Two Numbers:51 99
After Swapping two Numbers are:
Number1= 99
Number2= 51
import java.util.Scanner;
public class SwapTwoNumber { public static void main(String[] args) { Scanner sc = new Scanner(System.in); /* Get the two numbers input */ System.out.print("Enter the Two Numbers:"); int num1 = sc.nextInt(); int num2 = sc.nextInt(); /* Swaping two numbers using bitwise XOR operators. */ num1 = num1 ^ num2; /* ^(XOR operators) */ num2 = num1 ^ num2; num1 = num1 ^ num2; System.out.print("After Swapping two Numbers are:\nNumber1= " + num1 + "\nNumber2= " + num2); sc.close(); }}
Program in Python
Here is the source code of the Python Program to swap two numbers using bitwise operators.
Code:
# Get the two numbers inputnum1, num2= map(int, input("Enter the two Numbers:").split())#Swaping two numbers using bitwise XOR operators.num1 = num1 ^ num2 #^(XOR operators)num2 = num1 ^ num2num1 = num1 ^ num2print("After Swapping two Numbers are:","\nNumber1= ",num1,"\nNumber2= ",num2)
Input/Output:
Enter the two Numbers:145 368
After Swapping two Numbers are:
Number1= 368
Number2= 145
# Get the two numbers inputnum1, num2= map(int, input("Enter the two Numbers:").split())#Swaping two numbers using bitwise XOR operators.num1 = num1 ^ num2 #^(XOR operators)num2 = num1 ^ num2num1 = num1 ^ num2print("After Swapping two Numbers are:","\nNumber1= ",num1,"\nNumber2= ",num2)
0 Comments
Please do not Enter any spam link in the comment box