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