Open In App

Swap all odd and even bits

Last Updated : 23 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Given an unsigned integer N. The task is to swap all odd bits with adjacent even bits.

Examples: 

Input: 23
Output: 43
Explanation: 23 (00010111) should be converted to 43 (00101011).

Input: 2
Output: 1

Naive Approach: Below is the idea to solve the problem

For every even index i in the binary representation of N starting from index 0 swap bits with (i+1)th index.

Follow the steps below to implement the idea:

  • Find the bit at i and i+1 index.
  • To swap the bits subtract and add corresponding values.
  • To remove bit at ith bit to i+1. subtract i_bit<<i and add it at i+1 index for that we need to add i_bit<<(i+1).
  • Similarly subtract (i+1)th bit and add it to ith index.

Below is the implementation of above approach.

C++
// C++ program to swap even and
// odd bits of a given number
#include <bits/stdc++.h>
using namespace std;

// Function to swap even
// and odd bits
unsigned int swapBits(unsigned int x)
{
    for (int i = 0; i < 32; i += 2) {

        // Find i th bit
        int i_bit = (x >> i) & 1;

        // Find i+1 th bit
        int i_1_bit = (x >> (i + 1)) & 1;

        // Remove i_bit
        x = x
            - (i_bit << i)
            // Remove i+1 th bit
            - (i_1_bit << (i + 1))
            // Put i_bit at i+1 location
            + (i_bit << (i + 1))
            // Put i+1 bit at i location
            + (i_1_bit << i);
    }
    return x;
}

// Driver code
int main()
{
    // 00010111
    unsigned int x = 23;

    // Function Call
    cout << swapBits(x);

    return 0;
}

// This code is contributed by Amandeep Gupta
Java
// Java program to swap even and
// odd bits of a given number
import java.io.*;

class GFG {

    // Function to swap even
    // and odd bits
    static int swapBits(int x)
    {
        for (int i = 0; i < 32; i += 2) {
            int i_bit = (x >> i) & 1; // find i th bit
            int i_1_bit
                = (x >> (i + 1)) & 1; // find i+1 th bit

            x = x - (i_bit << i) // remove i_bit
                - (i_1_bit << (i + 1)) // remove i+1 th bit
                + (i_bit
                   << (i + 1)) // put i_bit at i+1 location
                + (i_1_bit
                   << i); // put i+1 bit at i location
        }
        return x;
    }

    // Driver code
    public static void main(String[] args)
    {
        int x = 23; // 00010111

        // Output is 43 (00101011)
        System.out.print(swapBits(x));
    }
}

// This code is contributed by subham348.
Python3
# Python program to swap even and
# odd bits of a given number

# Function to swap even
# and odd bits
def swapBits(x):
  
    # Get all even bits of x
    even_bits = x & 0xAAAAAAAA

    # Get all odd bits of x
    odd_bits = x & 0x55555555
    
    # Right shift even bits
    even_bits >>= 1
    
    # Left shift odd bits
    odd_bits <<= 1 
    for i in range(0,32,2):
        i_bit = (x >> 1) & 1; # find i th bit
        i_1_bit = (x >> (i + 1)) & 1; # find i+1 th bit
        
        x = x - (i_bit << i) # remove i_bit
        - (i_1_bit << (i + 2)) # remove i+1 th bit
        + (i_bit << (i + 1)) # put i_bit at i+1 location
        + (i_1_bit << i); # put i+1 bit at i location
    
    # Combine even and odd bits
    return (even_bits | odd_bits) 

# Driver code
if __name__ == '__main__':
    x = 23; # 00010111

    # Output is 43 (00101011)
    print(swapBits(x));


# This code is contributed by Rajput-Ji 
C#
// C# program to swap even and
// odd bits of a given number
using System;

class GFG {

    // Function to swap even
    // and odd bits
    static int swapBits(int x)
    {
        for (int i = 0; i < 32; i += 2) {
            int i_bit = (x >> i) & 1; // find i th bit
            int i_1_bit
                = (x >> (i + 1)) & 1; // find i+1 th bit

            x = x - (i_bit << i) // remove i_bit
                - (i_1_bit << (i + 1)) // remove i+1 th bit
                + (i_bit
                   << (i + 1)) // put i_bit at i+1 location
                + (i_1_bit
                   << i); // put i+1 bit at i location
        }
        return x;
    }

    // Driver code
    public static void Main()
    {
        int x = 23; // 00010111

        // Output is 43 (00101011)
        Console.Write(swapBits(x));
    }
}

// This code is contributed by subham348.
JavaScript
<script>
// JavaScript Program to implement
// the above approach

// Function to swap even
// and odd bits
function swapBits( x)
{
    for(let i = 0; i < 32; i += 2){
        let i_bit = ( x >> i ) & 1; // find i th bit
        let i_1_bit = (x >> ( i+1 )) & 1;  // find i+1 th bit
        
        x = x - ( i_bit << i) // remove i_bit
              - ( i_1_bit << ( i+1 ) ) // remove i+1 th bit
              + ( i_bit << ( i+1 ) ) // put i_bit at i+1 location
              + ( i_1_bit << i );  // put i+1 bit at i location
    }
    return x;
}

// Driver code
    let x =23; // 00010111

    // Output is 43 (00101011)
    document.write(swapBits(x));

    // This code is contributed by Potta Lokesh
    </script>

Output
43

Time Complexity: O(1)
Auxiliary Space: O(1)

Efficient Approach: Below is the idea to solve the problem.

The value even_bits obtained by even bits of N and Right shifted (>>) by 1 on even_bits and similarly obtain value odd_bits of odd bits of N and perform left shift (<<) by 1 operation on odd_bits. Now (odd_bits | even_bits) will give the desired value.

Follow the below steps to implement the approach:

  • Initialize variable even_bits with bitwise and of N with 0xAAAAAAAA(32 bit number with all even bits set as 1 and all odd bits as 0). 
  • Initialize variable odd_bits with bitwise and of N with 0x55555555. The number 0x55555555 is a 32 bit number with all odd bits set as 1 and all even bits as 0
  • Right shift even_bits by 1 and Left shift odd_bits by 1. 
  • Return or of even_bits with odd_bits 

Below is the Implementation of above approach.

C++
// C++ program to swap even and 
// odd bits of a given number 
#include <bits/stdc++.h>
using namespace std;

// Function to swap even 
// and odd bits 
unsigned int swapBits(unsigned int N) 
{ 
    // Get all even bits of x 
    unsigned int even_bits = N & 0xAAAAAAAA; 

    // Get all odd bits of x 
    unsigned int odd_bits = N & 0x55555555; 
    
      // Right shift even bits 
    even_bits >>= 1;
  
      // Left shift odd bits 
    odd_bits <<= 1; 

      // Combine even and odd bits 
    return (even_bits | odd_bits); 
} 

// Driver code
int main() 
{ 
      // 00010111 
    unsigned int N = 23; 

    //Function Call
    cout<<swapBits(N); 

    return 0; 
} 

// This code is contributed by rathbhupendra
C
// C program to swap even and 
// odd bits of a given number
#include <stdio.h>

// Function to swap even
// and odd bits
unsigned int swapBits(unsigned int x)
{
    // Get all even bits of x
    unsigned int even_bits = x & 0xAAAAAAAA; 

    // Get all odd bits of x
    unsigned int odd_bits  = x & 0x55555555; 

    even_bits >>= 1;  // Right shift even bits
    odd_bits <<= 1;   // Left shift odd bits

    return (even_bits | odd_bits); // Combine even and odd bits
}

// Driver program to test above function
int main()
{
    unsigned int x = 23; // 00010111

    // Output is 43 (00101011)
    printf("%u ", swapBits(x));

    return 0;
}
Java
// Java program to swap even 
// and odd bits of a given number

class GFG{
    
    // Function to swap even
    // and odd bits
    static int swapBits(int x)
    {
        // Get all even bits of x
        int even_bits = x & 0xAAAAAAAA; 
    
        // Get all odd bits of x
        int odd_bits = x & 0x55555555; 
    
        // Right shift even bits
        even_bits >>= 1; 
        
        // Left shift even bits
        odd_bits <<= 1; 
        
        // Combine even and odd bits
        return (even_bits | odd_bits); 
    }
    
    // Driver program to test above function
    public static void main(String[] args)
    {
        int x = 23; // 00010111
    
        // Output is 43 (00101011)
        System.out.println(swapBits(x));
    }
}

// This code is contributed by Smitha Dinesh Semwal
Python 3
# Python 3 program to swap even 
# and odd bits of a given number

# Function for swapping even 
# and odd bits
def swapBits(x) :
    
    # Get all even bits of x
    even_bits = x & 0xAAAAAAAA

    # Get all odd bits of x
    odd_bits = x & 0x55555555
    
    # Right shift even bits
    even_bits >>= 1
    
    # Left shift odd bits
    odd_bits <<= 1 

    # Combine even and odd bits
    return (even_bits | odd_bits) 


# Driver program
# 00010111
x = 23

# Output is 43 (00101011)
print(swapBits(x))


# This code is contributed 
# by Nikita Tiwari.
C#
// C# program to swap even and odd bits
// of a given number
using System;

class GFG {
    
    // Function to swap even
    // and odd bits
    static long swapBits(int x)
    {
        // Get all even bits of x
        long even_bits = x & 0xAAAAAAAA; 
    
        // Get all odd bits of x
        long odd_bits = x & 0x55555555; 
    
        // Right shift even bits
        even_bits >>= 1; 
        
        // Left shift even bits
        odd_bits <<= 1; 
        
        // Combine even and odd bits
        return (even_bits | odd_bits); 
    }
    
    // Driver program to test above function
    public static void Main()
    {
        
        int x = 23; // 00010111
    
        // Output is 43 (00101011)
        Console.Write(swapBits(x));
    }
}

// This code is contributed by Sam007.
PHP
<?php
// PHP program to swap even and 
// odd bits of a given number

// Function to swap even
// and odd bits
function swapBits( $x)
{
    
    // Get all even bits of x
    $even_bits = $x & 0xAAAAAAAA; 

    // Get all odd bits of x
    $odd_bits = $x & 0x55555555; 
    
    // Right shift even bits
    $even_bits >>= 1; 
    
    // Left shift odd bits
    $odd_bits <<= 1; 

    // Combine even and odd bits
    return ($even_bits | $odd_bits); 
}

// Driver Code

// 00010111
$x = 23; 

// Output is 43 (00101011)
echo swapBits($x);

// This code is contributed by Ajit
?>
JavaScript
<script>
// java script program to swap even and
// odd bits of a given number

// Function to swap even
// and odd bits
function swapBits( x)
{
    
    // Get all even bits of x
    even_bits = x & 0xAAAAAAAA;

    // Get all odd bits of x
    odd_bits = x & 0x55555555;
    
    // Right shift even bits
    even_bits >>= 1;
    
    // Left shift odd bits
    odd_bits <<= 1;

    // Combine even and odd bits
    return (even_bits | odd_bits);
}

// Driver Code

// 00010111
let x = 23;

// Output is 43 (00101011)
document.write(swapBits(x));

// This code is contributed by sravan kumar

</script>

Output
43

Time Complexity: O(1)
Auxiliary Space: O(1)
 


 


Swap all odd and even bits | DSA Problem
Article Tags :
Practice Tags :

Similar Reads