How to swap two bits in a given integer ? | C ++ code for swapping given bits of a number ; left - shift 1 p1 and p2 times and using XOR ; Driver Code
#include <iostream> NEW_LINE using namespace std ; int swapBits ( int n , int p1 , int p2 ) { n ^= 1 << p1 ; n ^= 1 << p2 ; return n ; } int main ( ) { cout << " Result ▁ = ▁ " << swapBits ( 28 , 0 , 3 ) ; return 0 ; }