Skip to content
Related Articles
Swap all odd and even bits
• Difficulty Level : Medium
• Last Updated : 06 Apr, 2021

Given an unsigned integer, swap all odd bits with even bits. For example, if the given number is 23 (00010111), it should be converted to 43 (00101011). Every even position bit is swapped with adjacent bit on right side (even position bits are highlighted in binary representation of 23), and every odd position bit is swapped with adjacent on left side.

If we take a closer look at the example, we can observe that we basically need to right shift (>>) all even bits (In the above example, even bits of 23 are highlighted) by 1 so that they become odd bits (highlighted in 43), and left shift (<<) all odd bits by 1 so that they become even bits. The following solution is based on this observation. The solution assumes that input number is stored using 32 bits.
Let the input number be x
1) Get all even bits of x by doing bitwise and of x with 0xAAAAAAAA. The number 0xAAAAAAAA is a 32 bit number with all even bits set as 1 and all odd bits as 0.
2) Get all odd bits of x by doing bitwise and of x with 0x55555555. The number 0x55555555 is a 32 bit number with all odd bits set as 1 and all even bits as 0.
3) Right shift all even bits.
4) Left shift all odd bits.
5) Combine new even and odd bits and return.

## C++

 `// C++ program to swap even and``// odd bits of a given number``#include ``using` `namespace` `std;` `// 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 code``int` `main()``{``    ``unsigned ``int` `x = 23; ``// 00010111` `    ``// Output is 43 (00101011)``    ``cout<

## C

 `// C program to swap even and``// odd bits of a given number``#include ` `// 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

 `>= 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

 ``

Output:

` 43 `

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live

My Personal Notes arrow_drop_up