Minimize bits to be flipped in X and Y such that their Bitwise OR is equal to Z | This function returns minimum number of bits to be flipped in X and Y to make X | Y = Z ; If current bit in Z is set and is also set in either of X or Y or both ; If current bit in Z is set and is unset in both X and Y ; Set that bit in either X or Y ; If current bit in Z is unset and is set in both X and Y ; Unset the bit in both X and Y ; If current bit in Z is unset and is set in either X or Y ; Unset that set bit ; Driver Code
function minimumFlips ( X , Y , Z ) { var res = 0 ; while ( X > 0 Y > 0 Z > 0 ) { if ( ( ( X & 1 ) || ( Y & 1 ) ) && ( Z & 1 ) ) { X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; continue ; } else if ( ( X & 1 ) == 0 && ( Y & 1 ) == 0 && ( Z & 1 ) ) { res ++ ; } else if ( ( X & 1 ) || ( Y & 1 ) == 1 ) { if ( ( X & 1 ) && ( Y & 1 ) && ( Z & 1 ) == 0 ) { res += 2 ; } else if ( ( ( X & 1 ) || ( Y & 1 ) ) && ( Z & 1 ) == 0 ) { res ++ ; } } X = X >> 1 ; Y = Y >> 1 ; Z = Z >> 1 ; } return res ; } var X = 5 , Y = 8 , Z = 6 ; document . write ( minimumFlips ( X , Y , Z ) ) ;