Change bits to make specific OR value | Returns max of three numbers ; Returns count of bits in N ; Returns bit at ' pos ' position ; Utility method to toggle bit at ' pos ' position ; method returns minimum number of bit flip to get T as OR value of A and B ; Loop over maximum number of bits among A , B and T ; T 's bit is set, try to toggle  bit of B, if not already ; if A 's bit is set,   flip that ; if B 's bit is set,  flip that ; if K is less than 0 then we can make A | B == T ; Loop over bits one more time to minimise A further ; If both bit are set , then Unset A 's bit to minimise it ; If A ' s ▁ bit ▁ is ▁ 1 ▁ and ▁ B ' s bit is 0 , toggle both ; Output changed value of A and B ; Driver Code
< ? php function maxDD ( $ a , $ b , $ c ) { return max ( $ a , ( max ( $ b , $ c ) ) ) ; } function bitCount ( $ N ) { $ cnt = 0 ; while ( $ N ) { $ cnt ++ ; $ N >>= 1 ; } return $ cnt ; } function at_position ( $ num , $ pos ) { $ bit = $ num & ( 1 << $ pos ) ; return $ bit ; } function toggle ( & $ num , $ pos ) { $ num ^= ( 1 << $ pos ) ; } function minChangeToReachTaregetOR ( $ A , $ B , $ K , $ T ) { $ maxlen = max ( bitCount ( $ A ) , bitCount ( $ B ) , bitCount ( $ T ) ) ; for ( $ i = $ maxlen - 1 ; $ i >= 0 ; $ i -- ) { $ bitA = at_position ( $ A , $ i ) ; $ bitB = at_position ( $ B , $ i ) ; $ bitT = at_position ( $ T , $ i ) ; if ( $ bitT ) { if ( ! $ bitA && ! $ bitB ) { toggle ( $ B , $ i ) ; $ K -- ; } } else { if ( $ bitA ) { toggle ( $ A , $ i ) ; $ K -- ; } if ( $ bitB ) { toggle ( $ B , $ i ) ; $ K -- ; } } } if ( $ K < 0 ) { echo " Not ▁ possible STRNEWLINE " ; return ; } for ( $ i = $ maxlen - 1 ; $ K > 0 && $ i >= 0 ; -- $ i ) { $ bitA = at_position ( $ A , $ i ) ; $ bitB = at_position ( $ B , $ i ) ; $ bitT = at_position ( $ T , $ i ) ; if ( $ bitT ) { if ( $ bitA && $ bitB ) { toggle ( $ A , $ i ) ; $ K -- ; } } if ( $ bitA && ! $ bitB && $ K >= 2 ) { toggle ( $ A , $ i ) ; toggle ( $ B , $ i ) ; $ K -= 2 ; } } echo $ A , " " ▁ , ▁ $ B ▁ , ▁ " " } $ A = 175 ; $ B = 66 ; $ K = 5 ; $ T = 100 ; minChangeToReachTaregetOR ( $ A , $ B , $ K , $ T ) ; ? >