Count set bits in Bitwise XOR of all adjacent elements upto N | Function to count of set bits in Bitwise XOR of adjacent elements up to N ; Stores count of set bits by Bitwise XOR on adjacent elements of [ 0 , N ] ; Stores all possible values on right most set bit over [ 0 , N ] ; Iterate over the range [ 0 , N ] ; Update N ; Update bit_Position ; Driver Code
function countXORSetBitsAdjElemRange1_N ( N ) { let total_set_bits = 0 ; let bit_Position = 1 ; while ( N ) { total_set_bits += ( Math . floor ( ( N + 1 ) / 2 ) * bit_Position ) ; N -= Math . floor ( ( N + 1 ) / 2 ) ; bit_Position ++ ; } return total_set_bits ; } let N = 4 ; document . write ( countXORSetBitsAdjElemRange1_N ( N ) ) ;