# Swap all even and odd bits of a number

The given problem requires swapping all even and odd bits of a given number. In binary representation, even bits are the positions with 0-based indexing 0, 2, 4, etc., and odd bits are the positions with 1-based indexing 1, 3, 5, etc. The task is to interchange the values of all even and odd bit positions in the binary representation of the given number.

## Problem Statement

The goal is to write an algorithm that takes an integer as input and returns a new integer with all even and odd bits swapped. We will represent the integer in binary form and perform the swapping operation on the binary representation.

## Explanation with an Example

Let's take an example to understand the problem better. Consider the input number 42 (in binary 101010). After swapping the even and odd bits, the output should be 21 (in binary 010101).

Binary representation of 42: 1 0 1 0 1 0 After swapping even and odd bits: 0 1 0 1 0 1

## Pseudocode

The following pseudocode represents the algorithm for swapping all even and odd bits of a given number:

``````1. Read the input number n.
2. Get all the even bits of n using bitwise AND operation with a mask that has 1's at even positions and 0's at odd positions. Shift this result right by 1 to bring it to odd positions.
3. Get all the odd bits of n using bitwise AND operation with a mask that has 1's at odd positions and 0's at even positions. Shift this result left by 1 to bring it to even positions.
4. Combine the results obtained in steps 2 and 3 using bitwise OR operation to get the final result.
5. Display the original number and the resultant number.
``````

## Algorithm with Explanation

1. Start
2. Read the input number n.
3. Create a mask for even bits and odd bits:
• Mask for even bits: 2863311530 (in binary 10101010101010101010101010101010)
• Mask for odd bits: 1431655765 (in binary 01010101010101010101010101010101)
4. Calculate the even bits by performing a bitwise AND operation between n and the mask for even bits. Then, right shift the result by 1 to bring it to odd positions. Store this result in the variable `even`.
5. Calculate the odd bits by performing a bitwise AND operation between n and the mask for odd bits. Then, left shift the result by 1 to bring it to even positions. Store this result in the variable `odd`.
6. Combine the `even` and `odd` values obtained in steps 4 and 5 using the bitwise OR operation. Store the result in the variable `result`.
7. Display the original number `n` and the resultant number `result`.
8. End

## Code Solution

Here given code implementation process.

``````// C Program
// Swap all odd and even bits
#include <stdio.h>

// Swap two bits in a given number
void swapEvenOddBits(int n)
{
// Get all even active bits
int even = (n & 2863311530) >> 1;
// Get all Odd active bits
int odd = (n & 1431655765) << 1;
int result = even | odd;
// Display calculated result
printf(" Number : %d", n);
printf("\n Output : %d\n\n", result);
}
int main()
{
// Test cases
// 010000 => 100000
swapEvenOddBits(16);
// 00100011 (35) =>  00010011 (19)
swapEvenOddBits(35);
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  1111110001 (-15) => 11110010 (-14)
swapEvenOddBits(-15);
// 1010 (10) => 0101
swapEvenOddBits(10);
return 0;
}``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : -15
Output : -14

Number : 10
Output : 5
``````
``````/*
Java Program for
Swap all odd and even bits
*/
public class BitExchange
{
// Swap two bits in a given number
public void swapEvenOddBits(int n)
{
// Get all even active bits
long even = (n & 2863311530L) >> 1;
// Get all Odd active bits
long odd =  (n & 1431655765) << 1;
long result = even | odd;
// Display calculated result
System.out.print(" Number : " + n);
System.out.print("\n Output : " + result + "\n\n");
}
public static void main(String[] args)
{
//  Test cases
//  010000 => 100000 (32)
//  00100011 (35) =>  00010011 (19)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  1111110001 (-15) => 11110010 (-14)
//  1010 (10) => 0101
}
}``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : -15
Output : -14

Number : 10
Output : 5
``````
``````// Include header file
#include <iostream>
using namespace std;

/*
C++ Program for
Swap all odd and even bits
*/

class BitExchange
{
public:
// Swap two bits in a given number
void swapEvenOddBits(int n)
{
// Get all even active bits
int even = (n & 2863311530) >> 1;
// Get all Odd active bits
int odd = (n & 1431655765) << 1;
int result = even | odd;
// Display calculated result
cout << " Number : " << n;
cout << "\n Output : " << result << "\n\n";
}
};
int main()
{
//  Test cases
//  010000 => 100000 (32)
//  00100011 (35) =>  00010011 (19)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  1111110001 (-15) => 11110010 (-14)
//  1010 (10) => 0101
return 0;
}``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : -15
Output : -14

Number : 10
Output : 5
``````
``````// Include namespace system
using System;
/*
C# Program for
Swap all odd and even bits
*/
public class BitExchange
{
// Swap two bits in a given number
public void swapEvenOddBits(int n)
{
// Get all even active bits
long even = (n & 2863311530) >> 1;
// Get all Odd active bits
long odd = (n & 1431655765) << 1;
long result = even | odd;
// Display calculated result
Console.Write(" Number : " + n);
Console.Write("\n Output : " + result + "\n\n");
}
public static void Main(String[] args)
{
//  Test cases
//  010000 => 100000 (32)
//  00100011 (35) =>  00010011 (19)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  1111110001 (-15) => 11110010 (-14)
//  1010 (10) => 0101
}
}``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : -15
Output : -14

Number : 10
Output : 5
``````
``````<?php
/*
Php Program for
Swap all odd and even bits
*/
class BitExchange
{
// Swap two bits in a given number
public	function swapEvenOddBits(\$n)
{
// Get all even active bits
\$even = (\$n & 2863311530) >> 1;
// Get all Odd active bits
\$odd =  (\$n & 1431655765) << 1;
\$result = \$even | \$odd;
// Display calculated result
echo " Number : ". \$n;
echo "\n Output : ". \$result ."\n\n";
}
}

function main()
{
//  Test cases
//  010000 => 100000 (32)
//  00100011 (35) =>  00010011 (19)
//  1010 (10) => 0101
}
main();``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : 10
Output : 5
``````
``````/*
Node Js Program for
Swap all odd and even bits
*/
class BitExchange
{
// Swap two bits in a given number
swapEvenOddBits(n)
{
// Get all even active bits
var even = (n & 2863311530) >> 1;
// Get all Odd active bits
var odd = (n & 1431655765) << 1;
var result = even | odd;
// Display calculated result
process.stdout.write(" Number : " + n);
process.stdout.write("\n Output : " + result + "\n\n");
}
}

function main()
{
//  Test cases
//  010000 => 100000 (32)
//  00100011 (35) =>  00010011 (19)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  1111110001 (-15) => 11110010 (-14)
//  1010 (10) => 0101
}
main();``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : -15
Output : -14

Number : 10
Output : 5
``````
``````#   Python 3 Program for
#   Swap all odd and even bits

class BitExchange :
#  Swap two bits in a given number
def swapEvenOddBits(self, n) :
#  Get all even active bits
even = (n & 2863311530) >> 1
#  Get all Odd active bits
odd = (n & 1431655765) << 1
result = even | odd
#  Display calculated result
print(" Number : ", n, end = "")
print("\n Output : ", result ,"\n")

def main() :
#   Test cases
#   010000 => 100000 (32)
#   00100011 (35) =>  00010011 (19)
#   1010 (10) => 0101

if __name__ == "__main__": main()``````

#### Output

`````` Number :  16
Output :  32

Number :  35
Output :  19

Number :  10
Output :  5
``````
``````#   Ruby Program for
#   Swap all odd and even bits

class BitExchange
#  Swap two bits in a given number
def swapEvenOddBits(n)
#  Get all even active bits
even = (n & 2863311530) >> 1
#  Get all Odd active bits
odd = (n & 1431655765) << 1
result = even | odd
#  Display calculated result
print(" Number : ", n)
print("\n Output : ", result ,"\n\n")
end

end

def main()
#   Test cases
#   010000 => 100000 (32)
#   00100011 (35) =>  00010011 (19)
#   1010 (10) => 0101
end

main()``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : 10
Output : 5

``````
``````/*
Scala Program for
Swap all odd and even bits
*/
class BitExchange
{
// Swap two bits in a given number
def swapEvenOddBits(n: Int): Unit = {
// Get all even active bits
var even: Long = (n & 2863311530L) >> 1;
// Get all Odd active bits
var odd: Long = (n & 1431655765) << 1;
var result: Long = even | odd;
// Display calculated result
print(" Number : " + n);
print("\n Output : " + result + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: BitExchange = new BitExchange();
//  Test cases
//  010000 => 100000 (32)
//  00100011 (35) =>  00010011 (19)
//  00001111 (15)  => 11110000 (1s) => (11110001) (2s)
//  1111110001 (-15) => 11110010 (-14)
//  1010 (10) => 0101
}
}``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : -15
Output : -14

Number : 10
Output : 5
``````
``````/*
Swift 4 Program for
Swap all odd and even bits
*/
class BitExchange
{
// Swap two bits in a given number
func swapEvenOddBits(_ n: Int)
{
// Get all even active bits
let even: Int = (n & 2863311530) >> 1;
// Get all Odd active bits
let odd: Int = (n & 1431655765) << 1;
let result: Int = even | odd;
// Display calculated result
print(" Number : ", n, terminator: "");
print("\n Output : ", result ,"\n");
}
}
func main()
{
//  Test cases
//  010000 => 100000 (32)
//  00100011 (35) =>  00010011 (19)
//  1010 (10) => 0101
}
main();``````

#### Output

`````` Number :  16
Output :  32

Number :  35
Output :  19

Number :  10
Output :  5
``````
``````/*
Kotlin Program for
Swap all odd and even bits
*/
class BitExchange
{
// Swap two bits in a given number
fun swapEvenOddBits(n: Long): Unit
{
// Get all even active bits
var even: Long = (n and 2863311530) shr 1;
// Get all Odd active bits
var odd: Long = (n and 1431655765) shl 1;
var result: Long = even or odd;
// Display calculated result
print(" Number : " + n);
print("\n Output : " + result + "\n\n");
}
}
fun main(args: Array <String> ): Unit
{
//  Test cases
//  010000 => 100000 (32)
//  00100011 (35) =>  00010011 (19)
//  1010 (10) => 0101
}``````

#### Output

`````` Number : 16
Output : 32

Number : 35
Output : 19

Number : 10
Output : 5
``````

## Output Explanation

Now, let's apply the algorithm to the provided test cases and explain the output:

1. Test case: `swapEvenOddBits(16)`

• Input: 16 (in binary 10000)
• Even bits: 00000 (right shift of 00000, which remains unchanged)
• Odd bits: 00000 (left shift of 00000, which remains unchanged)
• Result: 00000 (00000 OR 00000 = 00000)
• Output: Original Number: 16, Result: 0
2. Test case: `swapEvenOddBits(35)`

• Input: 35 (in binary 100011)
• Even bits: 00001 (right shift of 0010)
• Odd bits: 10000 (left shift of 0100)
• Result: 10001 (00001 OR 10000 = 10001)
• Output: Original Number: 35, Result: 17
3. Test case: `swapEvenOddBits(-15)`

• Input: -15 (in binary 11111111111111111111111111110001)
• Even bits: 1111111111111111111111111111000 (right shift of 1111111111111111111111111111000)
• Odd bits: 11111111111111111111111111110010 (left shift of 11111111111111111111111111110010)
• Result: 11111111111111111111111111111010 (1111111111111111111111111111000 OR 11111111111111111111111111110010 = 11111111111111111111111111111010)
• Output: Original Number: -15, Result: -10
4. Test case: `swapEvenOddBits(10)`

• Input: 10 (in binary 1010)
• Even bits: 0010 (right shift of 0010)
• Odd bits: 0101 (left shift of 0101)
• Result: 0111 (0010 OR 0101 = 0111)
• Output: Original Number: 10, Result: 7

## Time Complexity of the Code

The time complexity of the code is O(1) because it involves a fixed number of bitwise operations, and the number of bits in the input does not affect the complexity. Thus, the code runs in constant time, regardless of the input size.

## Finally

In this article, we explored the problem of swapping all even and odd bits of a given number. We discussed the problem statement, provided an explanation with a suitable example, and presented the pseudocode and algorithm to solve the problem. Furthermore, we explained the resultant output for each test case and analyzed the time complexity of the code. By swapping the even and odd bits using bitwise operations, we can efficiently solve this problem for any given integer.

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