How to swap two bits in a given integer ? | JavaScript code for swapping given bits of a number ; left - shift 1 p1 and p2 times and using XOR ; Driver code
function swapBits ( n , p1 , p2 ) { n ^= 1 << p1 ; n ^= 1 << p2 ; return n ; } document . write ( " " + swapBits ( 28 , 0 , 3 ) ) ;