Maximum Bitwise OR pair from a range | Javascript implementation of the approach ; Function to return the maximum bitwise OR possible among all the possible pairs ; If there is only a single value in the range [ L , R ] ; Loop through each bit from MSB to LSB ; MSBs where the bits differ , all bits from that bit are set ; If MSBs are same , then ans bit is same as that of bit of right or left limit ; Driver code
let MAX = 64 ; function maxOR ( L , R ) { if ( L == R ) { return L ; } let ans = 0 ; for ( let i = MAX - 1 ; i >= 0 ; i -- ) { let p , lbit , rbit ; p = 1 << i ; if ( ( rbit == 1 ) && ( lbit == 0 ) ) { ans += ( p << 1 ) - 1 ; break ; } if ( rbit == 1 ) { ans += p ; } } return ans ; } let L = 4 , R = 5 ; document . write ( maxOR ( L , R ) ) ;