Related Articles

Related Articles

Swap every two bits in bytes
  • Difficulty Level : Medium
  • Last Updated : 18 Apr, 2018

Swap all the pair of bits in a byte. Before swapping: 11-10-11-01 After swapping: 11-01-11-10

Examples:

Input  : 00000010
Output : 00000001

Input  : 00000100
Output : 00001000

Approach:
x = ((x & 0b10101010) >> 1) | ((x & 0b01010101) <> 1 extracts the high bit position and shifts it to the low bit position.
Similarly the expression (x & 0b01010101) << 1 extracts the low bit from each pair and shifts it to the high bit position.
The two parts are then combined using bitwise-OR.

x= 00011010
((x & 0b10101010) >> 1) = 00001010 >> 1
                        = 00000101
((x & 0b01010101) << 1) = 00010000 <> 1) | ((x & 0b01010101) << 1) = 00100101

Below is the implementation of the above:
Note: This solution works for only 8 bit.

C++



filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to swap every two bits in a byte.
#include<bits/stdc++.h>
using namespace std;
  
unsigned int swapBitsInPair(unsigned int x)
{
    // Extracting the high bit shift it to lowbit
    // Extracting the low bit shift it to highbit
    return ((x & 0b10101010) >> 1) | 
            ((x & 0b01010101) << 1);    
}
  
/* Driver function to test above function */
int main()
{
    unsigned int x = 4; 
    cout << swapBitsInPair(x);    
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program to swap every 
// two bits in a byte.
import java.util.*;
  
class GFG
{
    static int swapBitsInPair( int x)
    {
        // Extracting the high bit shift it to lowbit
        // Extracting the low bit shift it to highbit
        return ((x & 0b10101010) >> 1) | 
                ((x & 0b01010101) << 1); 
    }
  
    // Driver Function
    public static void main(String[] args)
    {
    int x = 4
    System.out.print(swapBitsInPair(x)); 
    }
}
  
// This code is contributed by Gitanjali.

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python program to swap every 
# two bits in a byte.
  
import math
  
def swapBitsInPair( x):
  
    # Extracting the high bit shift it to lowbit
    # Extracting the low bit shift it to highbit
    return ((x & 0b10101010) >> 1) or ((x & 0b01010101) << 1)
  
# driver Function
x = 4
print(swapBitsInPair(x)) 
  
# This code is contributed by Gitanjali.

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# program to swap every two bits in a byte.
using System;
  
public class GFG{
  
    static uint swapBitsInPair(uint x)
    {
        // Extracting the high bit shift it to lowbit
        // Extracting the low bit shift it to highbit
        return ((x & 010101010) >> 1) | 
                ((x & 001010101) << 1); 
    }
      
    // Driver function to test above function 
    static public void Main () {
          
        uint x = 4; 
          
        Console.WriteLine(swapBitsInPair(x)); 
    }
}
  
// This code is contributed by vt_m.

chevron_right


PHP

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
// PHP program to swap every
// two bits in a byte.
  
function swapBitsInPair($x)
{
      
    // Extracting the high bit 
    // shift it to lowbit
    // Extracting the low bit
    // shift it to highbit
    return (($x & 0b10101010) >> 1) | 
           (($x & 0b01010101) << 1); 
}
  
    // Driver Code
    $x = 4; 
    echo swapBitsInPair($x); 
  
// This code is contributed by mits 
?>

chevron_right



Output:

8

Reference:
https://stackoverflow.com/questions/4788799/swap-every-pair-of-bits-in-byte

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.




My Personal Notes arrow_drop_up
Recommended Articles
Page :