-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBitwiseFlipCounter.java
More file actions
33 lines (26 loc) · 887 Bytes
/
Copy pathBitwiseFlipCounter.java
File metadata and controls
33 lines (26 loc) · 887 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package LeetcodePractice;
public class BitwiseFlipCounter {
public int minFlips(int a, int b, int c) {
int flips = 0;
for (int i = 0; i < 32; i++) {
int aBit = (a >> i) & 1;
int bBit = (b >> i) & 1;
int cBit = (c >> i) & 1;
int orBit = aBit | bBit;
if (orBit != cBit) {
if (cBit == 1) {
flips += 1;
} else {
flips += aBit + bBit;
}
}
}
return flips;
}
public static void main(String[] args) {
BitwiseFlipCounter solution = new BitwiseFlipCounter();
System.out.println(solution.minFlips(2, 6, 5));
System.out.println(solution.minFlips(4, 2, 7));
System.out.println(solution.minFlips(1, 2, 3));
}
}