C Program to Swap Two Numbers Using Bitwise Operators

In this tutorial you will learn about the C Program to Swap Two Numbers Using Bitwise Operators and its application with practical example.

In this tutorial, we will learn to create a program to swap two numbers using bitwise XOR operator in C programming language.

Prerequisites

Before starting with this tutorial we assume that you are best aware of the following C programming topics:

  • C Data Types
  • C Variables
  • C Input Output
  • C Operators
  • C Bitwise Operators

Program to Swap Two Numbers Using Bitwise Operator

In this program we will swap two integer numbers entered by the user using bitwise XOR operator. We have first declared and initialized the required variables. Next, we would prompt user to input two integer numbers. Later in the program we will swap numbers using bitwise XOR operator. Finally, we will display number values after swapping of numbers.

Output:-

C-Program-to-Swap-Two-Numbers

In the above program, we have first declared and initialized a set variables required in the program.

  • num1 = it holds the first integer number value
  • num2 = it holds the second integer number value

In the next statement user will be prompted to enter the two integer number values which will be assigned to variable ‘num1’ and ‘num2’ respectively. Next, we will swap two integer numbers using bitwise XOR operator. Finally, we will display number values after swapping of numbers.

In this tutorial we have learn about the C Program to Swap Two Numbers Using Bitwise Operators and its application with practical example. I hope you will like this tutorial.