Swap two numbers using bitwise operator

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


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.

New Comment







© 2021, kalkicode.com, All rights reserved