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.
Next Steps:
- Get Free Certificate of Merit in C Programming
- Participate in C Programming Certification Contest
- Become a Top Ranker in C Programming
- Take C Programming Tests
- Chapterwise Practice Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
- Chapterwise Mock Tests: Chapter 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
Related Posts:
- Buy Computer Science Books
- Practice BCA MCQs
- Buy C Books
- Apply for C Internship
- Apply for Computer Science Internship