Swap every two bits in bytes

22 May, 2022
Swap all the pair of bits in a byte. Before swapping: 11-10-11-01 After swapping: 11-01-11-10Examples:  
Input  : 00000010
Output : 00000001

Input  : 00000100
Output : 00001000
 
Approach: x = ((x & 0x55555555) >> 1) | ((x & 0xAAAAAAAA) <> 1 extracts the high bit position and shifts it to the low bit position. Similarly the expression (x & 0xAAAAAAAA) << 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 & 0xAAAAAAAA) >> 1) = 00001010 >> 1
                        = 00000101
((x & 0x55555555) << 1) = 00010000 <> 1) | ((x & 0x55555555) << 1) = 00100101
Below is the implementation of the above idea:  
C++
Java
Python3
C#
PHP
Javascript
// 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 & 0xAAAAAAAA) >> 1) |            ((x & 0x55555555) << 1);   } /* Driver function to test above function */int main(){    unsigned int x = 4;    cout << swapBitsInPair(x);       return 0;} // This code is contributed by Kasina Dheeraj.
// 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 & 0xAAAAAAAA) >> 1) |                ((x & 0x55555555) << 1);    }     // Driver Function    public static void main(String[] args)    {    int x = 4;    System.out.print(swapBitsInPair(x));    }} // This code is contributed by Gitanjali.
# 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 & 0xAAAAAAAA) >> 1) or ((x & 0x55555555) << 1) # driver Functionx = 4;print(swapBitsInPair(x)) # This code is contributed by Gitanjali.
// 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 & 0xAAAAAAAA) >> 1) |                ((x & 0x55555555) << 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.
<?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 & 0xAAAAAAAA) >> 1) |           (($x & 0x55555555) << 1);}     // Driver Code    $x = 4;    echo swapBitsInPair($x); // This code is contributed by mits?>
<script> // java script 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 & 0xAAAAAAAA) >> 1) |           ((x & 0x55555555) << 1);}     // Driver Code    let x = 4;    document.write( swapBitsInPair(x)); // This code is contributed by sravan kumar</script>
Output:  
8
Time Complexity: O(1)
Space Complexity: O(1)
Reference: https://stackoverflow.com/questions/4788799/swap-every-pair-of-bits-in-byte
This article is contributed by Saumya and improved by Kasina Dheeraj .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
Mithun Kumar
sravankumar8128
dheerukd2002
Cisco
Bit Magic
Cisco
Bit Magic
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Bits manipulation (Important tactics)
Divide two integers without using multiplication, division and mod operator
Bit Fields in C
Add two numbers without using arithmetic operators
Find the element that appears once
C++ bitset and its application
Find the two non-repeating elements in an array of repeating elements/ Unique Numbers 2
Count total set bits in all numbers from 1 to n
Set, Clear and Toggle a given bit of a number in C
Position of rightmost set bit