Toggle all bits after most significant bit | Function to toggle bits starting from MSB ; Temporary variable to use XOR with one of a n ; Run loop until the only set bit in temp crosses MST of n . ; Toggle bit of n corresponding to current set bit in temp . ; Move set bit to next higher position . ; Driver code
function toggle ( n ) { let temp = 1 ; while ( temp <= n ) { n = n ^ temp ; temp = temp << 1 ; } return n ; } let n = 10 ; n = toggle ( n ) ; document . write ( n ) ;