Minimum number of bits required to be flipped such that Bitwise OR of A and B is equal to C | C ++ program for the above approach ; Function to count the number of bit flips required on A and B such that Bitwise OR of A and B is C ; Stores the count of flipped bit ; Iterate over the range [ 0 , 32 ] ; Check if i - th bit of A is set ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; If i - th bit of C is unset ; Check if i - th bit of A is set or not ; Check if i - th bit of B is set or not ; Check if i - th bit of C is set or not ; Check if i - th bit of both A and B is set ; Return the count of bits flipped ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int minimumFlips ( int A , int B , int C ) { int res = 0 ; for ( int i = 0 ; i < 32 ; i ++ ) { int x = 0 , y = 0 , z = 0 ; if ( A & ( 1 << i ) ) { x = 1 ; } if ( B & ( 1 << i ) ) { y = 1 ; } if ( C & ( 1 << i ) ) { z = 1 ; } if ( z == 0 ) { if ( x ) { res ++ ; } if ( y ) { res ++ ; } } if ( z == 1 ) { if ( x == 0 && y == 0 ) { res ++ ; } } } return res ; } int main ( ) { int A = 2 , B = 6 , C = 5 ; cout << minimumFlips ( A , B , C ) ; return 0 ; }