# Swap two numbers using bitwise operator

The problem at hand is to swap two numbers `a` and `b` without using a temporary variable, utilizing bitwise operators. Bitwise operators manipulate individual bits in a binary representation of numbers.

Problem Statement:

Given two integers `a` and `b`, we need to swap their values using bitwise operators.

Explanation with Suitable Example:

Let's take a simple example to understand the concept of swapping using bitwise operators. Consider `a = 5` and `b = 9`.

1. Binary Representation:

``````a = 0101
b = 1001``````

Standard Pseudocode:

``````1. Let 'a' and 'b' be the two numbers to be swapped.

2. Perform the following steps:
a. a = a XOR b
b. b = a XOR b
c. a = a XOR b

3. The values of 'a' and 'b' have been swapped.
``````

Algorithm Explanation:

1. First, initialize two variables `a` and `b` with the given numbers (e.g., `a = 10` and `b = 20`).

2. Print the initial values of `a` and `b`.

3. Perform the swap operation using bitwise XOR:

• `a = a ^ b`: The bitwise XOR operation between `a` and `b` stores the result in `a`.
• `b = a ^ b`: Now, `b` is updated with the value of `a`, which means `b` holds the initial value of `a`.
• `a = a ^ b`: Finally, `a` is updated with the value of `b`, which means `a` now holds the initial value of `b`.
4. Print the values of `a` and `b` after the swap.

## Code Solution

Here given code implementation process.

``````// C Program
// Swap two numbers using bitwise operator
#include <stdio.h>

int main(int argc, char const *argv[])
{
// Number
int a = 10;
int b = 20;
printf("\n Before Swap  a : %d  b : %d", a, b);
// Perform swap operation
a = a ^ b;
b = a ^ b;
a = a ^ b;
// After swaps
printf("\n After Swap   a : %d  b : %d", a, b);
return 0;
}``````

#### Output

`````` Before Swap  a : 10  b : 20
After Swap   a : 20  b : 10``````
``````/*
Java program
Swap two numbers using bitwise operator
*/
public class Swapping
{
public static void main(String[] args)
{
// Number
int a = 10;
int b = 20;
System.out.print("\n Before Swap a : " + a + " b : " + b);
// Perform swap operation
a = a ^ b;
b = a ^ b;
a = a ^ b;
// After swaps
System.out.print("\n  After Swap a : " + a + " b : " + b);
}
}``````

#### Output

`````` Before Swap a : 10 b : 20
After Swap a : 20 b : 10``````
``````// Include header file
#include <iostream>

using namespace std;
/*
C++ program
Swap two numbers using bitwise operator
*/
int main()
{
// Number
int a = 10;
int b = 20;
cout << "\n Before Swap a : " << a << " b : " << b;
// Perform swap operation
a = a ^ b;
b = a ^ b;
a = a ^ b;
// After swaps
cout << "\n  After Swap a : " << a << " b : " << b;
return 0;
}``````

#### Output

`````` Before Swap a : 10 b : 20
After Swap a : 20 b : 10``````
``````// Include namespace system
using System;
/*
C# program
Swap two numbers using bitwise operator
*/
public class Swapping
{
public static void Main(String[] args)
{
// Number
int a = 10;
int b = 20;
Console.Write("\n Before Swap a : " + a + " b : " + b);
// Perform swap operation
a = a ^ b;
b = a ^ b;
a = a ^ b;
// After swaps
Console.Write("\n  After Swap a : " + a + " b : " + b);
}
}``````

#### Output

`````` Before Swap a : 10 b : 20
After Swap a : 20 b : 10``````
``````<?php
/*
Php program
Swap two numbers using bitwise operator
*/
function main()
{
// Number
\$a = 10;
\$b = 20;
echo "\n Before Swap a : ". \$a ." b : ". \$b;
// Perform swap operation
\$a = \$a ^ \$b;
\$b = \$a ^ \$b;
\$a = \$a ^ \$b;
// After swaps
echo "\n  After Swap a : ". \$a ." b : ". \$b;
}
main();``````

#### Output

`````` Before Swap a : 10 b : 20
After Swap a : 20 b : 10``````
``````/*
Node Js program
Swap two numbers using bitwise operator
*/
function swapping()
{
// Number
var a = 10;
var b = 20;
process.stdout.write("\n Before Swap a : " + a + " b : " + b);
// Perform swap operation
a = a ^ b;
b = a ^ b;
a = a ^ b;
// After swaps
process.stdout.write("\n  After Swap a : " + a + " b : " + b);
}
swapping();``````

#### Output

`````` Before Swap a : 10 b : 20
After Swap a : 20 b : 10``````
``````#   Python 3 program
#   Swap two numbers using bitwise operator

def swapping() :
#  Number
a = 10
b = 20
print("\n Before Swap a : ", a ," b : ", b, end = "")
#  Perform swap operation
a = a ^ b
b = a ^ b
a = a ^ b
#  After swaps
print("\n  After Swap a : ", a ," b : ", b, end = "")

if __name__ == "__main__": swapping()``````

#### Output

`````` Before Swap a :  10  b :  20
After Swap a :  20  b :  10``````
``````#   Ruby program
#   Swap two numbers using bitwise operator

def swapping()
#  Number
a = 10
b = 20
print("\n Before Swap a : ", a ," b : ", b)
#  Perform swap operation
a = a ^ b
b = a ^ b
a = a ^ b
#  After swaps
print("\n  After Swap a : ", a ," b : ", b)
end

swapping()``````

#### Output

`````` Before Swap a : 10 b : 20
After Swap a : 20 b : 10``````
``````/*
Scala program
Swap two numbers using bitwise operator
*/

object Main
{
def main(args: Array[String]): Unit = {
// Number
var a: Int = 10;
var b: Int = 20;
print("\n Before Swap a : " + a + " b : " + b);
// Perform swap operation
a = a ^ b;
b = a ^ b;
a = a ^ b;
// After swaps
print("\n  After Swap a : " + a + " b : " + b);
}
}``````

#### Output

`````` Before Swap a : 10 b : 20
After Swap a : 20 b : 10``````
``````/*
Swift 4 program
Swap two numbers using bitwise operator
*/

func main()
{
// Number
var a: Int = 10;
var b: Int = 20;
print("\n Before Swap a : ", a ," b : ", b, terminator: "");
// Perform swap operation
a = a ^ b;
b = a ^ b;
a = a ^ b;
// After swaps
print("\n  After Swap a : ", a ," b : ", b, terminator: "");
}
main();``````

#### Output

`````` Before Swap a :  10  b :  20
After Swap a :  20  b :  10``````
``````/*
Kotlin program
Swap two numbers using bitwise operator
*/

fun main(args: Array <String> ): Unit
{
// Number
var a: Int = 10;
var b: Int = 20;
print("\n Before Swap a : " + a + " b : " + b);
// Perform swap operation
a = a xor b;
b = a xor b;
a = a xor b;
// After swaps
print("\n  After Swap a : " + a + " b : " + b);
}``````

#### Output

`````` Before Swap a : 10 b : 20
After Swap a : 20 b : 10``````

Resultant Output Explanation:

Using the provided code, let's see the output:

``````Before Swap  a : 10  b : 20
After Swap   a : 20  b : 10``````

As we can see, the values of `a` and `b` have been successfully swapped using bitwise XOR.

Time Complexity:

The time complexity of this code is O(1), which means it has constant time complexity. The bitwise XOR operation takes a constant amount of time to perform regardless of the input values because it directly manipulates the individual bits of the numbers. Hence, the time complexity remains constant.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.