Sum of XOR of all subarrays | Function to calculate the sum of XOR of all subarrays ; variable to store the final sum ; multiplier ; variable to store number of sub - arrays with odd number of elements with ith bits starting from the first element to the end of the array ; variable to check the status of the odd - even count while calculating c_odd ; loop to calculate initial value of c_odd ; loop to iterate through all the elements of the array and update sum ; updating the multiplier ; returning the sum ; Driver Code
< ? php function findXorSum ( $ arr , $ n ) { $ sum = 0 ; $ mul = 1 ; for ( $ i = 0 ; $ i < 30 ; $ i ++ ) { $ c_odd = 0 ; $ odd = 0 ; for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { if ( ( $ arr [ $ j ] & ( 1 << $ i ) ) > 0 ) $ odd = ( ! $ odd ) ; if ( $ odd ) $ c_odd ++ ; } for ( $ j = 0 ; $ j < $ n ; $ j ++ ) { $ sum += ( $ mul * $ c_odd ) ; if ( ( $ arr [ $ j ] & ( 1 << $ i ) ) > 0 ) $ c_odd = ( $ n - $ j - $ c_odd ) ; } $ mul *= 2 ; } return $ sum ; } $ arr = array ( 3 , 8 , 13 ) ; $ n = sizeof ( $ arr ) ; echo findXorSum ( $ arr , $ n ) ; ? >