Swap two bits in an integer
The problem involves swapping two specific bits of a given integer. This operation is performed by manipulating the individual bits of the integer using bitwise operations. In this article, we'll explore the concept of swapping two bits in an integer using bitwise operations.
Problem Statement and Description
Given a positive integer n
and two bit positions a
and b
, the goal is to swap
the bits at positions a
and b
in the binary representation of the integer n
.
This means that the bit at position a
will take the value of the bit at position b
, and
vice versa.
Example
Consider the following examples to understand the concept:

For the number
35
(binary00100011
): Swapping the bits at positions6
and0
results in the binary number01100010
, which is98
in decimal. 
For the number
16
(binary10000
): Swapping the bits at positions1
and4
results in the binary number00010
, which is2
in decimal. 
For the number
15
(binary11110001
): Swapping the bits at positions5
and1
results in the binary number11010011
, which is45
in decimal. 
For the number
10
(binary1010
): Swapping the bits at positions1
and3
does not result in any change, as both bits have the same value.
Idea to Solve the Problem
The idea to solve the "Swap two bits in an integer" problem involves the following steps:
 Extract the bit values at positions
a
andb
using bitwise AND operations and right shifts.  Check if the extracted bit values are different. If they are different, perform a bitwise XOR operation on the
integer to swap the bits at positions
a
andb
.
Standard Pseudocode
function swapBits(num, a, b):
if a or b is negative:
Print "Invalid bit position (a, b)"
return
b1 = 1 & (num >> a)
b2 = 1 & (num >> b)
result = num
if b1 is not equal to b2:
result = num XOR ((1 << a) OR (1 << b))
Print "Number :", num
Print "Swap bits : (a, b)"
Print "Output :", result
Algorithm Explanation
 Check if
a
orb
is negative. If so, print an error message and return.  Extract the values of the bits at positions
a
andb
using bitwise AND operations and right shifts.  Check if the extracted bit values are different (
b1 != b2
). If they are different, perform a bitwise XOR operation between the integer and a value obtained by performing bitwise OR operations on the original integer with the values1 << a
and1 << b
. This effectively swaps the bits at positionsa
andb
.  Print the original number, the bit positions to be swapped, and the resulting number after swapping.
Code Solution

1) Swap two specific bits of a integer in java
2) Swap two specific bits of a integer in c++
3) C program to swap two bits
4) Swap two specific bits of a integer in c#
5) Swap two bits of a integer in python
6) Swap bits in a given number in ruby
7) Swap two specific bits of a integer in swift
8) Swap two specific bits of a integer in kotlin
9) Swap two given bits of an integer in php
10) Swap two specific bits of a integer in node js
11) Swap two specific bits of a integer in typescript
12) Swap two specific bits of a integer in vb.net
13) Golang program to swap two bits of a byte
14) Swap two bits of integer at given position in scala
Time Complexity
The time complexity of the provided algorithm is O(1), as it involves a constant number of bitwise operations regardless of the value of the given number.
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