This C Program Swaps two Numbers using Bitwise operators.
Here is source code of the C Program to Swap two Numbers using Bitwise operators. The C program is successfully compiled and run on a Linux system. The program output is also shown below.
/*
* C Program to Swap two Numbers using Bitwise operators
*/
#include <stdio.h>
#include <string.h>
/* Function 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);
}
/* Code to swap two numbers using bitwise operator */
void swap(int *x, int *y)
{
*x = *x ^ *y;
*y = *x ^ *y;
*x = *x ^ *y;
}
$ cc bit27.c $ a.out Enter two numbers:45 76 The numbers before swapping are Number1= 45 Number2=76 The numbers after swapping are Number1= 76 Number2=45
Sanfoundry Global Education & Learning Series – 1000 C Programs.
advertisement
advertisement
Here’s the list of Best Books in C Programming, Data-Structures and Algorithms
If you wish to look at programming examples on all topics, go to C Programming Examples.
Related Posts:
- Apply for C Internship
- Practice BCA MCQs
- Watch Advanced C Programming Videos
- Apply for Computer Science Internship
- Check C Books