Home » C programs » C bitwise operator's programs

# C program to swap two Integers using Bitwise Operators

In this article, we are going to see **how to swap two no using bitwise operator without using any temporary operators**?

Submitted by Radib Kar, on January 06, 2019

**Problem statement:** Write a **C program to swap two integers using bitwise operators without using any temporary variable**.

**Algorithm:**

- Let n1 and n2 be two numbers to be swapped.
- Update n1 to n1^n2, i.e., n1=n1^n2
- Update n2 to n1^n2, i.e., n2=n1^n2 //n1 is already updated in previous step, use the updated value.
- Again update n1 to n1^n2, i.e., n1=n1^n2 //n2 already updated in previous step.

This results in **swapping as n1 contains new value of n2 and vice versa**.

**Reason:**

n1=n1^n2; //statement1 n2=n1^n2; //statement2 n1=n1^n2; //statement3 At statement 2 replace n1 by stamen 1, n2 = (n1^n2) ^ n2 = n1^ (n2^n2) //XOR follows associative property = n1^0 // (n2^n2=0) = n1 At statement 3 replace n1, n2 by statement 1, 2 n1 = (n1^n2) ^ n1 //n2=n1 just found previously = (n2^n1) ^n1 //XOR follows commutative property = n2^ (n1^n1) = n2^ 0 = n2Thus n1 & n2 is swapped

**Example with explanation**

Let two numbers be n1= 5 //0000 0101 n2= 8 //0000 1000 Let's perform the aforementioned steps: 1. n1=n1^n2 n1= 0000 0101 ^ 0000 1000 = 0000 1101 2. n2=n1^n2 n2= 0000 1101 ^ 0000 1000= 0000 0101 //5=n1(original value of n1) actually 3. n1= n1^ n2 n1= 0000 1101 ^ 0000 0101 = 0000 1000 //8 =n2 (original value of n2) actually

### C implementation to swap two Integers using Bitwise Operators

#include <stdio.h> int main() { int n1,n2; printf("enter two numbers\n"); scanf("%d %d",&n1,&n2); printf("before swapping...\n"); printf("first no is %d, second no %d\n",n1,n2); //swapping using bitwise operators n1=n1^n2; n2=n1^n2; n1=n1^n2; //n1 & n2 is swapped printf("after swapping...\n"); printf("first no is %d, second no %d\n",n1,n2); return 0; }

**Output**

enter two numbers 5 7 before swapping... first no is 5, second no 7 after swapping... first no is 7, second no 5

TOP Interview Coding Problems/Challenges

- Run-length encoding (find/print frequency of letters in a string)
- Sort an array of 0's, 1's and 2's in linear time complexity
- Checking Anagrams (check whether two string is anagrams or not)
- Relative sorting algorithm
- Finding subarray with given sum
- Find the level in a binary tree with given sum K
- Check whether a Binary Tree is BST (Binary Search Tree) or not
- 1[0]1 Pattern Count
- Capitalize first and last letter of each word in a line
- Print vertical sum of a binary tree
- Print Boundary Sum of a Binary Tree
- Reverse a single linked list
- Greedy Strategy to solve major algorithm problems
- Job sequencing problem
- Root to leaf Path Sum
- Exit Point in a Matrix
- Find length of loop in a linked list
- Toppers of Class
- Print All Nodes that don't have Sibling
- Transform to Sum Tree
- Shortest Source to Destination Path

Comments and Discussions

**Ad:**
Are you a blogger? Join our Blogging forum.