Bitwise OR ( | ) of all even number from 1 to N | Function to return the bitwise OR of all even numbers upto N ; For value less than 2 ; Count total number of bits in bitwise or all bits will be set except last bit ; Compute 2 to the power bitCount and subtract 2 ; Driver code
function bitwiseOrTillN ( n ) { if ( n < 2 ) return 0 ; var bitCount = parseInt ( Math . log2 ( n ) + 1 ) ; return Math . pow ( 2 , bitCount ) - 2 ; } var n = 10 ; document . write ( bitwiseOrTillN ( n ) ) ;