Editorial Staff - - C Programming Tutorial
In this program, we are going to share a C program to swap two numbers using Bitwise Operators. If you are a beginner and want to start learning the C programming, then keep your close attention in this tutorial as I am going to share a C program to swap two numbers using Bitwise Operators with the output.
We have designed this program for beginners for learning purpose. Copy below c program and execute it with c compiler to see the output of the program.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #include <stdio.h> #include <string.h> //define the prototype void swap(int*, int *); void main() { int num1, num2; printf("\nEnter two numbers:"); scanf("%d %d", &num1, &num2); printf("\nThe numbers before swapping are Number1= %d Number2 = %d", num1, num2); swap(&num1, &num2); /* Call by Reference to function swap */ printf("\nThe numbers after swapping are Number1= %d Number2 = %d", num1, num2); } void swap(int *x, int *y) { *x = *x ^ *y; *y = *x ^ *y; *x = *x ^ *y; } |
Enter two numbers:131
89
The numbers before swapping are Number1= 131 Number2 = 89
The numbers after swapping are Number1= 89 Number2 = 131
Editorial Staff at FreeWebMentor is a team of professional developers leads by Prem Tiwari View all posts by Editorial Staff