1318. Minimum Flips to Make a OR b Equal to c
Given 3 positives numbers a, b and c. Return the minimum flips required in some bits of a and b to make ( a OR b == c ). (bitwise OR operation).
Flip operation consists of change any single bit 1 to 0 or change the bit 0 to 1 in their binary representation.
Example 1:

Input: a = 2, b = 6, c = 5
Output: 3
Explanation: After flips a = 1 , b = 4 , c = 5 such that (a OR b == c)
Example 2:
Input: a = 4, b = 2, c = 7
Output: 1
Example 3:
Input: a = 1, b = 2, c = 3
Output: 0
Constraints:
- 1 <= a <= 10^9
- 1 <= b <= 10^9
- 1 <= c <= 10^9
From: LeetCode
Link: 1318. Minimum Flips to Make a OR b Equal to c
Solution:
Ideas:
For each bit position:
- When the corresponding bit of c is 0, both bits in a and b must be 0.
- Add one flip for every bit that is currently 1.
- When the corresponding bit of c is 1, at least one of the bits in a or b must be 1.
- If both are 0, one flip is required.
Code:
c
int minFlips(int a, int b, int c) {
int flips = 0;
while (a != 0 || b != 0 || c != 0) {
int bitA = a & 1;
int bitB = b & 1;
int bitC = c & 1;
if (bitC == 0) {
// Both bits must be 0.
flips += bitA + bitB;
} else {
// At least one bit must be 1.
if (bitA == 0 && bitB == 0) {
flips++;
}
}
a >>= 1;
b >>= 1;
c >>= 1;
}
return flips;
}