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 arr1 [ ] ; Size of N ; Function Call
def XOR_for_every_i ( A , N ) : NEW_LINE INDENT frequency_of_bits = [ 0 ] * 32 NEW_LINE for i in range ( N ) : NEW_LINE INDENT bit_position = 0 NEW_LINE M = A [ i ] NEW_LINE while ( M ) : NEW_LINE INDENT if ( M & 1 != 0 ) : NEW_LINE INDENT frequency_of_bits [ bit_position ] += 1 NEW_LINE DEDENT bit_position += 1 NEW_LINE M >>= 1 NEW_LINE DEDENT DEDENT for i in range ( N ) : NEW_LINE INDENT M = A [ i ] NEW_LINE value_at_that_bit = 1 NEW_LINE XOR_sum = 0 NEW_LINE for bit_position in range ( 32 ) : NEW_LINE INDENT if ( M & 1 != 0 ) : NEW_LINE INDENT XOR_sum += ( ( N - frequency_of_bits [ bit_position ] ) * value_at_that_bit ) NEW_LINE DEDENT else : NEW_LINE INDENT XOR_sum += ( ( frequency_of_bits [ bit_position ] ) * value_at_that_bit ) NEW_LINE DEDENT M >>= 1 NEW_LINE value_at_that_bit <<= 1 NEW_LINE DEDENT print ( XOR_sum , end = " ▁ " ) NEW_LINE DEDENT return NEW_LINE DEDENT A = [ 1 , 2 , 3 ] NEW_LINE N = len ( A ) NEW_LINE XOR_for_every_i ( A , N ) NEW_LINE