Sum of Bitwise XOR of each array element with all other array elements | Function to calculate for each array element , sum of its Bitwise XOR with all other array elements ; Declare an array of size 64 to store count of each bit ; Traversing the array ; Check if bit is present of not ; Increase the bit position ; Reduce the number to half ; Traverse the array ; Stores the bit position ; Stores the sum of Bitwise XOR ; Check if bit is present of not ; Reduce the number to its half ; Print the sum for A [ i ] ; Given array ; Given N ; Function Call
function XOR_for_every_i ( A , N ) { let frequency_of_bits = new Uint8Array ( 32 ) ; for ( let i = 0 ; i < N ; i ++ ) { let bit_position = 0 ; let M = A [ i ] ; while ( M != 0 ) { if ( ( M & 1 ) != 0 ) { frequency_of_bits [ bit_position ] += 1 ; } bit_position += 1 ; M >>= 1 ; } } for ( let i = 0 ; i < N ; i ++ ) { let M = A [ i ] ; let value_at_that_bit = 1 ; let XOR_sum = 0 ; for ( let bit_position = 0 ; bit_position < 32 ; bit_position ++ ) { if ( ( M & 1 ) != 0 ) { XOR_sum += ( N - frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } else { XOR_sum += ( frequency_of_bits [ bit_position ] ) * value_at_that_bit ; } M >>= 1 ; value_at_that_bit <<= 1 ; } document . write ( XOR_sum + " " ) ; } return ; } let A = [ 1 , 2 , 3 ] ; let N = A . length ; XOR_for_every_i ( A , N ) ;