Swap all odd and even bits using C++



We can swap all odd and even bits in a given integer. Swapping of odd and even bits means changing the bits present at odd positions with the bits at even positions in a binary representation of a number. One real-life application is optimizing data storage or modifying data patterns in memory.

In this article, we are going to learn how we can swap all odd and even bits of a number in C++ using different approaches.

Formula for Swapping Odd and Even Bits

  • Extract all even-positioned bits using a bit mask and shift them right.
  • Extract all odd-positioned bits using a bit mask and shift them left.
  • Combine both values using the bitwise OR (|) operator.

Here are two examples based on the above formula:

Example 1

Input: n = 23 Output: Swapped number = 43 Explanation: Binary representation of 23 is 00010111. After swapping odd and even places: It becomes 00101011, which is 43 in decimal. 

Example 2

Input: n = 10 Output: Swapped number = 5 Explanation: Binary representation of 10 is 00001010. After swapping: It becomes 00000101, which is 5 in decimal. 

Below are different approaches to swap all odd and even bits of a number:

Using Bit Manipulation

In this approach, we use bitwise operators to extract, shift, and combine bits efficiently.

  • Define the integer number.
  • Now, extract even bits using 0xAAAAAAAA and shift them right by 1.
  • Now, extract odd bits using 0x55555555 and shift them left by 1.
  • Combine these using the OR (|) operator.
  • Finally, output the swapped number.

Example

Here is an example code implementing above steps to swap odd and even bits using bit manipulation.

#include<bits/stdc++.h> using namespace std; unsigned int swapOddEvenBits(unsigned int n) { unsigned int even_bits = n & 0xAAAAAAAA; // Extract even bits unsigned int odd_bits = n & 0x55555555; // Extract odd bits even_bits >>= 1; // Right shift even bits odd_bits <<= 1; // Left shift odd bits return (even_bits | odd_bits); // Combine both } int main() { unsigned int n = 23; cout << "Swapped number: " << swapOddEvenBits(n); return 0; } 

Using Loops

In this approach, we manually swap bits by iterating through each bit pair.

  • First, extract each bit pair using bitwise AND masks.
  • Now, swap and shift them accordingly.
  • Combine the answer.
  • Output the swapped number.

Example

The following example code uses loops to swap odd and even bits.

#include<bits/stdc++.h> using namespace std; unsigned int swapBitsLoop(unsigned int n) { unsigned int result = 0; for (int i = 0; i < 32; i += 2) { unsigned int even_bit = (n & (1 << i)) << 1; unsigned int odd_bit = (n & (1 << (i + 1))) >> 1; result |= even_bit | odd_bit; } return result; } int main() { unsigned int n = 10; cout << "Swapped number: " << swapBitsLoop(n); return 0; } 

Using XOR and Shift Operations

This is a more optimized approach that reduces redundant bitwise operations by using XOR and shifting.

  • First, extract even bits using a mask.
  • Now, extract odd bits using another mask.
  • Swap them using XOR and shift operations.
  • Output the answer.

Example

Here is an example code implementing XOR and shift operations to swap odd and even bits.

#include<bits/stdc++.h> using namespace std; unsigned int swapBitsOptimized(unsigned int n) { return ((n & 0xAAAAAAAA) >> 1) | ((n & 0x55555555) << 1); } int main() { unsigned int n = 42; cout << "Swapped number: " << swapBitsOptimized(n); return 0; } 

Complexity Comparison

Here is a comparison of time and space complexity of all the above approaches.

Approach Time Complexity Space Complexity
Bit Manipulation O(1) O(1)
Loops O(log N) O(1)
XOR and Shift O(1) O(1)

Real-Life Applications

  • It is used in data encoding and cryptography.
  • It is used for helping in graphics rendering to manipulate pixel data.
  • It is used for optimizing hardware memory management in embedded systems.
  • It is used in parallel computing for efficient data processing.
Updated on: 2025-04-22T15:11:28+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements