Sum of Bitwise AND of each array element with the elements of another array | Function to compute the AND sum for each element of an array ; Declaring an array of size 32 for storing the count of each bit ; Traverse the array arr2 [ ] and store the count of a bit in frequency array ; Current bit position ; While num is greater than 0 ; Checks if ith bit is set or not ; Increment the count of bit by one ; Increment the bit position by one ; Right shift the num by one ; Traverse in the arr2 [ ] ; Store the ith bit value ; Total required sum ; Traverse in the range [ 0 , 31 ] ; Checks if current bit is set ; Increment the bitwise sum by frequency [ bit_position ] * value_at_that_bit ; ; Right shift num by one ; Left shift vale_at_that_bit by one ; Print the sum obtained for ith number in arr1 [ ] ; Given arr1 [ ] ; Given arr2 [ ] ; Size of arr1 [ ] ; Size of arr2 [ ] ; Function Call
function Bitwise_AND_sum_i ( arr1 , arr2 , M , N ) { let frequency = new Array ( 32 ) . fill ( 0 ) ; for ( let i = 0 ; i < N ; i ++ ) { let bit_position = 0 ; let num = arr1 [ i ] ; while ( num ) { if ( num & 1 ) { frequency [ bit_position ] += 1 ; } bit_position += 1 ; num >>= 1 ; } } for ( let i = 0 ; i < M ; i ++ ) { let num = arr2 [ i ] ; let value_at_that_bit = 1 ; let bitwise_AND_sum = 0 ; for ( let bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( num & 1 ) { bitwise_AND_sum += frequency [ bit_position ] * value_at_that_bit ; } num >>= 1 ; value_at_that_bit <<= 1 ; } document . write ( bitwise_AND_sum + ' ' ) ; } return ; } let arr1 = [ 1 , 2 , 3 ] ; let arr2 = [ 1 , 2 , 3 ] ; let N = arr1 . length ; let M = arr2 . length Bitwise_AND_sum_i ( arr1 , arr2 , M , N ) ;