Posted on by Kalkicode
Code Bit Logic

# 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` (binary `00100011`): Swapping the bits at positions `6` and `0` results in the binary number `01100010`, which is `98` in decimal.

• For the number `16` (binary `10000`): Swapping the bits at positions `1` and `4` results in the binary number `00010`, which is `2` in decimal.

• For the number `-15` (binary `11110001`): Swapping the bits at positions `5` and `1` results in the binary number `11010011`, which is `-45` in decimal.

• For the number `10` (binary `1010`): Swapping the bits at positions `1` and `3` 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:

1. Extract the bit values at positions `a` and `b` using bitwise AND operations and right shifts.
2. 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` and `b`.

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

1. Check if `a` or `b` is negative. If so, print an error message and return.
2. Extract the values of the bits at positions `a` and `b` using bitwise AND operations and right shifts.
3. 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 values `1 << a` and `1 << b`. This effectively swaps the bits at positions `a` and `b`.
4. Print the original number, the bit positions to be swapped, and the resulting number after swapping.

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

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

Categories
Relative Post