# 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)
{
// Test cases
// (16) Change bit (1,4)
// 1000 => 0010 (2)
// (35) Change bit (6,0)
// 00100011 (35) =>  01100010 (98)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  11110001 (-15) => Change bit (5,1)
//  11110001 (-15) => 11010011 (-45)
// 1010 (10) Change bit (1,3)
// Bit value of 1 and 3 are similar
}
}``````

#### 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()
{
// Test cases
// (16) Change bit (1,4)
// 1000 => 0010 (2)
// (35) Change bit (6,0)
// 00100011 (35) =>  01100010 (98)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  11110001 (-15) => Change bit (5,1)
//  11110001 (-15) => 11010011 (-45)
// 1010 (10) Change bit (1,3)
// Bit value of 1 and 3 are similar
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)
{
// Test cases
// (16) Change bit (1,4)
// 1000 => 0010 (2)
// (35) Change bit (6,0)
// 00100011 (35) =>  01100010 (98)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  11110001 (-15) => Change bit (5,1)
//  11110001 (-15) => 11010011 (-45)
// 1010 (10) Change bit (1,3)
// Bit value of 1 and 3 are similar
}
}``````

#### 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()
{
// Test cases
// (16) Change bit (1,4)
// 1000 => 0010 (2)
// (35) Change bit (6,0)
// 00100011 (35) =>  01100010 (98)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  11110001 (-15) => Change bit (5,1)
//  11110001 (-15) => 11010011 (-45)
// 1010 (10) Change bit (1,3)
// Bit value of 1 and 3 are similar
}
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()
{
// Test cases
// (16) Change bit (1,4)
// 1000 => 0010 (2)
// (35) Change bit (6,0)
// 00100011 (35) =>  01100010 (98)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  11110001 (-15) => Change bit (5,1)
//  11110001 (-15) => 11010011 (-45)
// 1010 (10) Change bit (1,3)
// Bit value of 1 and 3 are similar
}
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() :
#  Test cases
#  (16) Change bit (1,4)
#  1000 => 0010 (2)
#  (35) Change bit (6,0)
#  00100011 (35) =>  01100010 (98)
#   00001111 (15)  => 11110000 (1s) => (11110001) (2s)
#   11110001 (-15) => Change bit (5,1)
#   11110001 (-15) => 11010011 (-45)
#  1010 (10) Change bit (1,3)
#  Bit value of 1 and 3 are similar

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()
#  Test cases
#  (16) Change bit (1,4)
#  1000 => 0010 (2)
#  (35) Change bit (6,0)
#  00100011 (35) =>  01100010 (98)
#   00001111 (15)  => 11110000 (1s) => (11110001) (2s)
#   11110001 (-15) => Change bit (5,1)
#   11110001 (-15) => 11010011 (-45)
#  1010 (10) Change bit (1,3)
#  Bit value of 1 and 3 are similar
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)
// (35) Change bit (6,0)
// 00100011 (35) =>  01100010 (98)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  11110001 (-15) => Change bit (5,1)
//  11110001 (-15) => 11010011 (-45)
// 1010 (10) Change bit (1,3)
// Bit value of 1 and 3 are similar
}
}``````

#### 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()
{
// Test cases
// (16) Change bit (1,4)
// 1000 => 0010 (2)
// (35) Change bit (6,0)
// 00100011 (35) =>  01100010 (98)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  11110001 (-15) => Change bit (5,1)
//  11110001 (-15) => 11010011 (-45)
// 1010 (10) Change bit (1,3)
// Bit value of 1 and 3 are similar
}
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
{
// Test cases
// (16) Change bit (1,4)
// 1000 => 0010 (2)
// (35) Change bit (6,0)
// 00100011 (35) =>  01100010 (98)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  11110001 (-15) => Change bit (5,1)
//  11110001 (-15) => 11010011 (-45)
// 1010 (10) Change bit (1,3)
// Bit value of 1 and 3 are similar
}``````

#### 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.