Toggle bits in the given range | function to toggle bits in the given range ; calculating a number ' num ' having ' r ' number of bits and bits in the range l to r are the only set bits ; toggle bits in the range l to r in ' n ' and return the number Besides this , we can calculate num as : num = ( 1 << r ) - l . ; Driver program to test above
function toggleBitsFromLToR ( n , l , r ) { var num = ( ( 1 << r ) - 1 ) ^ ( ( 1 << ( l - 1 ) ) - 1 ) ; return ( n ^ num ) ; } var n = 50 ; var l = 2 , r = 5 ; document . write ( toggleBitsFromLToR ( n , l , r ) ) ;