Swap all odd and even bits
Last Updated :
22 Nov, 2022
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++
#include <bits/stdc++.h>
using namespace std;
unsigned int swapBits(unsigned int x)
{
for ( int i = 0; i < 32; i += 2) {
int i_bit = (x >> i) & 1;
int i_1_bit = (x >> (i + 1)) & 1;
x = x
- (i_bit << i)
- (i_1_bit << (i + 1))
+ (i_bit << (i + 1))
+ (i_1_bit << i);
}
return x;
}
int main()
{
unsigned int x = 23;
cout << swapBits(x);
return 0;
}
|
Java
import java.io.*;
class GFG {
static int swapBits( int x)
{
for ( int i = 0 ; i < 32 ; i += 2 ) {
int i_bit = (x >> i) & 1 ;
int i_1_bit
= (x >> (i + 1 )) & 1 ;
x = x - (i_bit << i)
- (i_1_bit << (i + 1 ))
+ (i_bit
<< (i + 1 ))
+ (i_1_bit
<< i);
}
return x;
}
public static void main(String[] args)
{
int x = 23 ;
System.out.print(swapBits(x));
}
}
|
Python3
def swapBits(x):
even_bits = x & 0xAAAAAAAA
odd_bits = x & 0x55555555
even_bits >> = 1
odd_bits << = 1
for i in range ( 0 , 32 , 2 ):
i_bit = (x >> 1 ) & 1 ;
i_1_bit = (x >> (i + 1 )) & 1 ;
x = x - (i_bit << i)
- (i_1_bit << (i + 2 ))
+ (i_bit << (i + 1 ))
+ (i_1_bit << i);
return (even_bits | odd_bits)
if __name__ = = '__main__' :
x = 23 ;
print (swapBits(x));
|
C#
using System;
class GFG {
static int swapBits( int x)
{
for ( int i = 0; i < 32; i += 2) {
int i_bit = (x >> i) & 1;
int i_1_bit
= (x >> (i + 1)) & 1;
x = x - (i_bit << i)
- (i_1_bit << (i + 1))
+ (i_bit
<< (i + 1))
+ (i_1_bit
<< i);
}
return x;
}
public static void Main()
{
int x = 23;
Console.Write(swapBits(x));
}
}
|
Javascript
<script>
function swapBits( x)
{
for (let i = 0; i < 32; i += 2){
let i_bit = ( x >> i ) & 1;
let i_1_bit = (x >> ( i+1 )) & 1;
x = x - ( i_bit << i)
- ( i_1_bit << ( i+1 ) )
+ ( i_bit << ( i+1 ) )
+ ( i_1_bit << i );
}
return x;
}
let x =23;
document.write(swapBits(x));
</script>
|
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++
#include <bits/stdc++.h>
using namespace std;
unsigned int swapBits(unsigned int N)
{
unsigned int even_bits = N & 0xAAAAAAAA;
unsigned int odd_bits = N & 0x55555555;
even_bits >>= 1;
odd_bits <<= 1;
return (even_bits | odd_bits);
}
int main()
{
unsigned int N = 23;
cout<<swapBits(N);
return 0;
}
|
C
#include <stdio.h>
unsigned int swapBits(unsigned int x)
{
unsigned int even_bits = x & 0xAAAAAAAA;
unsigned int odd_bits = x & 0x55555555;
even_bits >>= 1;
odd_bits <<= 1;
return (even_bits | odd_bits);
}
int main()
{
unsigned int x = 23;
printf ( "%u " , swapBits(x));
return 0;
}
|
Java
class GFG{
static int swapBits( int x)
{
int even_bits = x & 0xAAAAAAAA ;
int odd_bits = x & 0x55555555 ;
even_bits >>= 1 ;
odd_bits <<= 1 ;
return (even_bits | odd_bits);
}
public static void main(String[] args)
{
int x = 23 ;
System.out.println(swapBits(x));
}
}
|
Python 3
def swapBits(x) :
even_bits = x & 0xAAAAAAAA
odd_bits = x & 0x55555555
even_bits >> = 1
odd_bits << = 1
return (even_bits | odd_bits)
x = 23
print (swapBits(x))
|
C#
using System;
class GFG {
static long swapBits( int x)
{
long even_bits = x & 0xAAAAAAAA;
long odd_bits = x & 0x55555555;
even_bits >>= 1;
odd_bits <<= 1;
return (even_bits | odd_bits);
}
public static void Main()
{
int x = 23;
Console.Write(swapBits(x));
}
}
|
PHP
<?php
function swapBits( $x )
{
$even_bits = $x & 0xAAAAAAAA;
$odd_bits = $x & 0x55555555;
$even_bits >>= 1;
$odd_bits <<= 1;
return ( $even_bits | $odd_bits );
}
$x = 23;
echo swapBits( $x );
?>
|
Javascript
<script>
function swapBits( x)
{
even_bits = x & 0xAAAAAAAA;
odd_bits = x & 0x55555555;
even_bits >>= 1;
odd_bits <<= 1;
return (even_bits | odd_bits);
}
let x = 23;
document.write(swapBits(x));
</script>
|
Time Complexity: O(1)
Auxiliary Space: O(1)
Share your thoughts in the comments
Please Login to comment...