Swap two bits in an integer

Here given code implementation process.

// C Program 
// Swap two bits in an integer
#include <stdio.h>

// Swap two bits in a given number
void swapBits(int n, int a, int b)
{
	if (a < 0 || b < 0)
	{
		printf("\n Invalid bit position (%d,%d)", a, b);
		return;
	}
	// Get first bit
	int b1 = 1 & (n >> a);
	// Get Second bit
	int b2 = 1 & (n >> b);
	int result = n;
	if (b1 != b2)
	{
		// When b1 and b2 not same
		// Swap bits
		result = n ^ ((1 << a) | (1 << b));
	}
	// Display calculated result
	printf(" Number : %d", n);
	printf("\n Swap bits : (%d,%d)", a, b);
	printf("\n Output : %d\n\n", result);
}
int main()
{
	// Test cases
	// (16) Change bit (1,4) 
	// 1000 => 0010 (2)
	swapBits(16, 1, 4);
	// (35) Change bit (6,0)
	// 00100011 (35) =>  01100010 (98)
	swapBits(35, 6, 0);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  11110001 (-15) => Change bit (5,1)
	//  11110001 (-15) => 11010011 (-45)
	swapBits(-15, 5, 1);
	// 1010 (10) Change bit (1,3)
	// Bit value of 1 and 3 are similar
	swapBits(10, 1, 3);
	return 0;
}

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 10
/*
  Java Program for
  Swap two bits in an integer
*/
public class BitExchange
{
	// Swap two bits in a given number
	public void swapBits(int n, int a, int b)
	{
		if (a < 0 || b < 0)
		{
			System.out.print("\n Invalid bit position (" + a + "," + b + ")");
			return;
		}
		// Get first bit
		int b1 = 1 & (n >> a);
		// Get Second bit
		int b2 = 1 & (n >> b);
		int result = n;
		if (b1 != b2)
		{
			// When b1 and b2 not same
			// Swap bits
			result = n ^ ((1 << a) | (1 << b));
		}
		// Display calculated result
		System.out.print(" Number : " + n + "");
		System.out.print("\n Swap bits : (" + a + "," + b + ")");
		System.out.print("\n Output : " + result + "\n\n");
	}
	public static void main(String[] args)
	{
		BitExchange task = new BitExchange();
		// Test cases
		// (16) Change bit (1,4) 
		// 1000 => 0010 (2)
		task.swapBits(16, 1, 4);
		// (35) Change bit (6,0)
		// 00100011 (35) =>  01100010 (98)
		task.swapBits(35, 6, 0);
		//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
		//  11110001 (-15) => Change bit (5,1)
		//  11110001 (-15) => 11010011 (-45)
		task.swapBits(-15, 5, 1);
		// 1010 (10) Change bit (1,3)
		// Bit value of 1 and 3 are similar
		task.swapBits(10, 1, 3);
	}
}

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 10
// Include header file
#include <iostream>

using namespace std;
/*
  C++ Program for
  Swap two bits in an integer
*/
class BitExchange
{
	public:
		// Swap two bits in a given number
		void swapBits(int n, int a, int b)
		{
			if (a < 0 || b < 0)
			{
				cout << "\n Invalid bit position (" << a << "," << b << ")";
				return;
			}
			// Get first bit
			int b1 = 1 &(n >> a);
			// Get Second bit
			int b2 = 1 &(n >> b);
			int result = n;
			if (b1 != b2)
			{
				// When b1 and b2 not same
				// Swap bits
				result = n ^ ((1 << a) | (1 << b));
			}
			// Display calculated result
			cout << " Number : " << n << "";
			cout << "\n Swap bits : (" << a << "," << b << ")";
			cout << "\n Output : " << result << "\n\n";
		}
};
int main()
{
	BitExchange task = BitExchange();
	// Test cases
	// (16) Change bit (1,4)
	// 1000 => 0010 (2)
	task.swapBits(16, 1, 4);
	// (35) Change bit (6,0)
	// 00100011 (35) =>  01100010 (98)
	task.swapBits(35, 6, 0);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  11110001 (-15) => Change bit (5,1)
	//  11110001 (-15) => 11010011 (-45)
	task.swapBits(-15, 5, 1);
	// 1010 (10) Change bit (1,3)
	// Bit value of 1 and 3 are similar
	task.swapBits(10, 1, 3);
	return 0;
}

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 10
// Include namespace system
using System;
/*
  C# Program for
  Swap two bits in an integer
*/
public class BitExchange
{
	// Swap two bits in a given number
	public void swapBits(int n, int a, int b)
	{
		if (a < 0 || b < 0)
		{
			Console.Write("\n Invalid bit position (" + a + "," + b + ")");
			return;
		}
		// Get first bit
		int b1 = 1 & (n >> a);
		// Get Second bit
		int b2 = 1 & (n >> b);
		int result = n;
		if (b1 != b2)
		{
			// When b1 and b2 not same
			// Swap bits
			result = n ^ ((1 << a) | (1 << b));
		}
		// Display calculated result
		Console.Write(" Number : " + n + "");
		Console.Write("\n Swap bits : (" + a + "," + b + ")");
		Console.Write("\n Output : " + result + "\n\n");
	}
	public static void Main(String[] args)
	{
		BitExchange task = new BitExchange();
		// Test cases
		// (16) Change bit (1,4)
		// 1000 => 0010 (2)
		task.swapBits(16, 1, 4);
		// (35) Change bit (6,0)
		// 00100011 (35) =>  01100010 (98)
		task.swapBits(35, 6, 0);
		//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
		//  11110001 (-15) => Change bit (5,1)
		//  11110001 (-15) => 11010011 (-45)
		task.swapBits(-15, 5, 1);
		// 1010 (10) Change bit (1,3)
		// Bit value of 1 and 3 are similar
		task.swapBits(10, 1, 3);
	}
}

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 10
<?php
/*
  Php Program for
  Swap two bits in an integer
*/
class BitExchange
{
	// Swap two bits in a given number
	public	function swapBits($n, $a, $b)
	{
		if ($a < 0 || $b < 0)
		{
			echo "\n Invalid bit position (". $a .",". $b .")";
			return;
		}
		// Get first bit
		$b1 = 1 & ($n >> $a);
		// Get Second bit
		$b2 = 1 & ($n >> $b);
		$result = $n;
		if ($b1 != $b2)
		{
			// When b1 and b2 not same
			// Swap bits
			$result = $n ^ ((1 << $a) | (1 << $b));
		}
		// Display calculated result
		echo " Number : ". $n ."";
		echo "\n Swap bits : (". $a .",". $b .")";
		echo "\n Output : ". $result ."\n\n";
	}
}

function main()
{
	$task = new BitExchange();
	// Test cases
	// (16) Change bit (1,4)
	// 1000 => 0010 (2)
	$task->swapBits(16, 1, 4);
	// (35) Change bit (6,0)
	// 00100011 (35) =>  01100010 (98)
	$task->swapBits(35, 6, 0);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  11110001 (-15) => Change bit (5,1)
	//  11110001 (-15) => 11010011 (-45)
	$task->swapBits(-15, 5, 1);
	// 1010 (10) Change bit (1,3)
	// Bit value of 1 and 3 are similar
	$task->swapBits(10, 1, 3);
}
main();

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 10
/*
  Node Js Program for
  Swap two bits in an integer
*/
class BitExchange
{
	// Swap two bits in a given number
	swapBits(n, a, b)
	{
		if (a < 0 || b < 0)
		{
			process.stdout.write("\n Invalid bit position (" + a + "," + b + ")");
			return;
		}
		// Get first bit
		var b1 = 1 & (n >> a);
		// Get Second bit
		var b2 = 1 & (n >> b);
		var result = n;
		if (b1 != b2)
		{
			// When b1 and b2 not same
			// Swap bits
			result = n ^ ((1 << a) | (1 << b));
		}
		// Display calculated result
		process.stdout.write(" Number : " + n + "");
		process.stdout.write("\n Swap bits : (" + a + "," + b + ")");
		process.stdout.write("\n Output : " + result + "\n\n");
	}
}

function main()
{
	var task = new BitExchange();
	// Test cases
	// (16) Change bit (1,4)
	// 1000 => 0010 (2)
	task.swapBits(16, 1, 4);
	// (35) Change bit (6,0)
	// 00100011 (35) =>  01100010 (98)
	task.swapBits(35, 6, 0);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  11110001 (-15) => Change bit (5,1)
	//  11110001 (-15) => 11010011 (-45)
	task.swapBits(-15, 5, 1);
	// 1010 (10) Change bit (1,3)
	// Bit value of 1 and 3 are similar
	task.swapBits(10, 1, 3);
}
main();

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 10
#   Python 3 Program for
#   Swap two bits in an integer

class BitExchange :
	#  Swap two bits in a given number
	def swapBits(self, n, a, b) :
		if (a < 0 or b < 0) :
			print("\n Invalid bit position (", a ,",", b ,")", end = "")
			return
		
		#  Get first bit
		b1 = 1 & (n >> a)
		#  Get Second bit
		b2 = 1 & (n >> b)
		result = n
		if (b1 != b2) :
			#  When b1 and b2 not same
			#  Swap bits
			result = n ^ ((1 << a) | (1 << b))
		
		#  Display calculated result
		print(" Number : ", n ,"", end = "")
		print("\n Swap bits : (", a ,",", b ,")", end = "")
		print("\n Output : ", result ,"\n")
	

def main() :
	task = BitExchange()
	#  Test cases
	#  (16) Change bit (1,4) 
	#  1000 => 0010 (2)
	task.swapBits(16, 1, 4)
	#  (35) Change bit (6,0)
	#  00100011 (35) =>  01100010 (98)
	task.swapBits(35, 6, 0)
	#   00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	#   11110001 (-15) => Change bit (5,1)
	#   11110001 (-15) => 11010011 (-45)
	task.swapBits(-15, 5, 1)
	#  1010 (10) Change bit (1,3)
	#  Bit value of 1 and 3 are similar
	task.swapBits(10, 1, 3)

if __name__ == "__main__": main()

Output

 Number :  16
 Swap bits : ( 1 , 4 )
 Output :  2

 Number :  35
 Swap bits : ( 6 , 0 )
 Output :  98

 Number :  -15
 Swap bits : ( 5 , 1 )
 Output :  -45

 Number :  10
 Swap bits : ( 1 , 3 )
 Output :  10
#   Ruby Program for
#   Swap two bits in an integer

class BitExchange 
	#  Swap two bits in a given number
	def swapBits(n, a, b) 
		if (a < 0 || b < 0) 
			print("\n Invalid bit position (", a ,",", b ,")")
			return
		end

		#  Get first bit
		b1 = 1 & (n >> a)
		#  Get Second bit
		b2 = 1 & (n >> b)
		result = n
		if (b1 != b2) 
			#  When b1 and b2 not same
			#  Swap bits
			result = n ^ ((1 << a) | (1 << b))
		end

		#  Display calculated result
		print(" Number : ", n ,"")
		print("\n Swap bits : (", a ,",", b ,")")
		print("\n Output : ", result ,"\n\n")
	end

end

def main() 
	task = BitExchange.new()
	#  Test cases
	#  (16) Change bit (1,4) 
	#  1000 => 0010 (2)
	task.swapBits(16, 1, 4)
	#  (35) Change bit (6,0)
	#  00100011 (35) =>  01100010 (98)
	task.swapBits(35, 6, 0)
	#   00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	#   11110001 (-15) => Change bit (5,1)
	#   11110001 (-15) => 11010011 (-45)
	task.swapBits(-15, 5, 1)
	#  1010 (10) Change bit (1,3)
	#  Bit value of 1 and 3 are similar
	task.swapBits(10, 1, 3)
end

main()

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 10

/*
  Scala Program for
  Swap two bits in an integer
*/
class BitExchange
{
	// Swap two bits in a given number
	def swapBits(n: Int, a: Int, b: Int): Unit = {
		if (a < 0 || b < 0)
		{
			print("\n Invalid bit position (" + a + "," + b + ")");
			return;
		}
		// Get first bit
		var b1: Int = 1 & (n >> a);
		// Get Second bit
		var b2: Int = 1 & (n >> b);
		var result: Int = n;
		if (b1 != b2)
		{
			// When b1 and b2 not same
			// Swap bits
			result = n ^ ((1 << a) | (1 << b));
		}
		// Display calculated result
		print(" Number : " + n + "");
		print("\n Swap bits : (" + a + "," + b + ")");
		print("\n Output : " + result + "\n\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: BitExchange = new BitExchange();
		// Test cases
		// (16) Change bit (1,4)
		// 1000 => 0010 (2)
		task.swapBits(16, 1, 4);
		// (35) Change bit (6,0)
		// 00100011 (35) =>  01100010 (98)
		task.swapBits(35, 6, 0);
		//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
		//  11110001 (-15) => Change bit (5,1)
		//  11110001 (-15) => 11010011 (-45)
		task.swapBits(-15, 5, 1);
		// 1010 (10) Change bit (1,3)
		// Bit value of 1 and 3 are similar
		task.swapBits(10, 1, 3);
	}
}

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 10
/*
  Swift 4 Program for
  Swap two bits in an integer
*/
class BitExchange
{
	// Swap two bits in a given number
	func swapBits(_ n: Int, _ a: Int, _ b: Int)
	{
		if (a < 0 || b < 0)
		{
			print("\n Invalid bit position (", a ,",", b ,")", terminator: "");
			return;
		}
		// Get first bit
		let b1: Int = 1 & (n >> a);
		// Get Second bit
		let b2: Int = 1 & (n >> b);
		var result: Int = n;
		if (b1  != b2)
		{
			// When b1 and b2 not same
			// Swap bits
			result = n ^ ((1 << a) | (1 << b));
		}
		// Display calculated result
		print(" Number : ", n ,"", terminator: "");
		print("\n Swap bits : (", a ,",", b ,")", terminator: "");
		print("\n Output : ", result ,"\n");
	}
}
func main()
{
	let task: BitExchange = BitExchange();
	// Test cases
	// (16) Change bit (1,4)
	// 1000 => 0010 (2)
	task.swapBits(16, 1, 4);
	// (35) Change bit (6,0)
	// 00100011 (35) =>  01100010 (98)
	task.swapBits(35, 6, 0);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  11110001 (-15) => Change bit (5,1)
	//  11110001 (-15) => 11010011 (-45)
	task.swapBits(-15, 5, 1);
	// 1010 (10) Change bit (1,3)
	// Bit value of 1 and 3 are similar
	task.swapBits(10, 1, 3);
}
main();

Output

 Number :  16
 Swap bits : ( 1 , 4 )
 Output :  2

 Number :  35
 Swap bits : ( 6 , 0 )
 Output :  98

 Number :  -15
 Swap bits : ( 5 , 1 )
 Output :  -45

 Number :  10
 Swap bits : ( 1 , 3 )
 Output :  10
/*
  Kotlin Program for
  Swap two bits in an integer
*/
class BitExchange
{
	// Swap two bits in a given number
	fun swapBits(n: Int, a: Int, b: Int): Unit
	{
		if (a < 0 || b < 0)
		{
			print("\n Invalid bit position (" + a + "," + b + ")");
			return;
		}
		// Get first bit
		var b1: Int = 1 and(n shr a);
		// Get Second bit
		var b2: Int = 1 and(n shr b);
		var result: Int = n;
		if (b1 != b2)
		{
			// When b1 and b2 not same
			// Swap bits
			result = n xor((1 shl a) or(1 shl b));
		}
		// Display calculated result
		print(" Number : " + n + "");
		print("\n Swap bits : (" + a + "," + b + ")");
		print("\n Output : " + result + "\n\n");
	}
}
fun main(args: Array <String> ): Unit
{
	var task: BitExchange = BitExchange();
	// Test cases
	// (16) Change bit (1,4)
	// 1000 => 0010 (2)
	task.swapBits(16, 1, 4);
	// (35) Change bit (6,0)
	// 00100011 (35) =>  01100010 (98)
	task.swapBits(35, 6, 0);
	//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
	//  11110001 (-15) => Change bit (5,1)
	//  11110001 (-15) => 11010011 (-45)
	task.swapBits(-15, 5, 1);
	// 1010 (10) Change bit (1,3)
	// Bit value of 1 and 3 are similar
	task.swapBits(10, 1, 3);
}

Output

 Number : 16
 Swap bits : (1,4)
 Output : 2

 Number : 35
 Swap bits : (6,0)
 Output : 98

 Number : -15
 Swap bits : (5,1)
 Output : -45

 Number : 10
 Swap bits : (1,3)
 Output : 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